---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E731 [*] Do not assign a `lambda` expression, use a `def`
 --> E731.py:3:5
  |
1 | def scope():
2 |     # E731
3 |     f = lambda x: 2 * x
  |     ^^^^^^^^^^^^^^^^^^^
  |
help: Rewrite `f` as a `def`
  |
2 |     # E731
  -     f = lambda x: 2 * x
3 +     def f(x):
4 +         return 2 * x
5 |
  |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
 --> E731.py:8:5
  |
6 | def scope():
7 |     # E731
8 |     f = lambda x: 2 * x
  |     ^^^^^^^^^^^^^^^^^^^
  |
help: Rewrite `f` as a `def`
   |
7  |     # E731
   -     f = lambda x: 2 * x
8  +     def f(x):
9  +         return 2 * x
10 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:14:9
   |
12 |     # E731
13 |     while False:
14 |         this = lambda y, z: 2 * x
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `this` as a `def`
   |
13 |     while False:
   -         this = lambda y, z: 2 * x
14 +         def this(y, z):
15 +             return 2 * x
16 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:19:5
   |
17 | def scope():
18 |     # E731
19 |     f = lambda: (yield 1)
   |     ^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `f` as a `def`
   |
18 |     # E731
   -     f = lambda: (yield 1)
19 +     def f():
20 +         return (yield 1)
21 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:24:5
   |
22 | def scope():
23 |     # E731
24 |     f = lambda: (yield from g())
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `f` as a `def`
   |
23 |     # E731
   -     f = lambda: (yield from g())
24 +     def f():
25 +         return (yield from g())
26 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:57:5
   |
55 | class Scope:
56 |     # E731
57 |     f = lambda x: 2 * x
   |     ^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `f` as a `def`
   |
56 |     # E731
   -     f = lambda x: 2 * x
57 +     def f(x):
58 +         return 2 * x
59 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:73:9
   |
71 |     x: Callable[[int], int]
72 |     if True:
73 |         x = lambda: 1
   |         ^^^^^^^^^^^^^
74 |     else:
75 |         x = lambda: 2
   |
help: Rewrite `x` as a `def`
   |
72 |     if True:
   -         x = lambda: 1
73 +         def x():
74 +             return 1
75 |     else:
   |
note: This is a display-only fix and is likely to be incorrect

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:75:9
   |
73 |         x = lambda: 1
74 |     else:
75 |         x = lambda: 2
   |         ^^^^^^^^^^^^^
76 |     return x
   |
help: Rewrite `x` as a `def`
   |
74 |     else:
   -         x = lambda: 2
75 +         def x():
76 +             return 2
77 |     return x
   |
note: This is a display-only fix and is likely to be incorrect

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:86:5
   |
84 |     # ParamSpec cannot be used in this context, so do not preserve the annotation.
85 |     P = ParamSpec("P")
86 |     f: Callable[P, int] = lambda *args: len(args)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `f` as a `def`
   |
85 |     P = ParamSpec("P")
   -     f: Callable[P, int] = lambda *args: len(args)
86 +     def f(*args) -> int:
87 +         return len(args)
88 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
  --> E731.py:94:5
   |
92 |     from typing import Callable
93 |
94 |     f: Callable[[], None] = lambda: None
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: Rewrite `f` as a `def`
   |
93 |
   -     f: Callable[[], None] = lambda: None
94 +     def f() -> None:
95 +         return None
96 |
   |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:102:5
    |
100 |     from typing import Callable
101 |
102 |     f: Callable[..., None] = lambda a, b: None
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f` as a `def`
    |
101 |
    -     f: Callable[..., None] = lambda a, b: None
102 +     def f(a, b) -> None:
103 +         return None
104 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:110:5
    |
108 |     from typing import Callable
109 |
110 |     f: Callable[[int], int] = lambda x: 2 * x
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f` as a `def`
    |
109 |
    -     f: Callable[[int], int] = lambda x: 2 * x
110 +     def f(x: int) -> int:
111 +         return 2 * x
112 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:119:5
    |
117 |     from collections.abc import Callable
118 |
119 |     f: Callable[[str, int], str] = lambda a, b: a * b
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f` as a `def`
    |
118 |
    -     f: Callable[[str, int], str] = lambda a, b: a * b
119 +     def f(a: str, b: int) -> str:
120 +         return a * b
121 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:127:5
    |
125 |     from collections.abc import Callable
126 |
127 |     f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f` as a `def`
    |
126 |
    -     f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
127 +     def f(a: str, b: int) -> tuple[str, int]:
128 +         return (a, b)
129 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:135:5
    |
133 |     from collections.abc import Callable
134 |
135 |     f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f` as a `def`
    |
134 |
    -     f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
135 +     def f(a: str, b: int, /, c: list[str]) -> list[str]:
136 +         return [*c, a * b]
137 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:139:5
    |
138 | class TemperatureScales(Enum):
139 |     CELSIUS = (lambda deg_c: deg_c)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    |
help: Rewrite `CELSIUS` as a `def`
    |
138 | class TemperatureScales(Enum):
    -     CELSIUS = (lambda deg_c: deg_c)
139 +     def CELSIUS(deg_c):
140 +         return (deg_c)
141 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:140:5
    |
138 | class TemperatureScales(Enum):
139 |     CELSIUS = (lambda deg_c: deg_c)
140 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `FAHRENHEIT` as a `def`
    |
139 |     CELSIUS = (lambda deg_c: deg_c)
    -     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
140 +     def FAHRENHEIT(deg_c):
141 +         return (deg_c * 9 / 5 + 32)
142 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:147:5
    |
145 |       # E731
146 |
147 | /     f = lambda: (
148 | |         i := 1,
149 | |     )
    | |_____^
    |
help: Rewrite `f` as a `def`
    |
146 |
    -     f = lambda: (
    -         i := 1,
    -     )
147 +     def f():
148 +         return (
149 +             i := 1,
150 +         )
151 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:163:1
    |
161 |   # Regression tests for:
162 |   # * https://github.com/astral-sh/ruff/issues/7720
163 | / x = lambda: """
164 | |         a
165 | |     b
166 | | """
    | |___^
167 |
168 |   # * https://github.com/astral-sh/ruff/issues/10277
    |
help: Rewrite `x` as a `def`
    |
162 | # * https://github.com/astral-sh/ruff/issues/7720
    - x = lambda: """
163 + def x():
164 +     return """
165 |         a
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:169:1
    |
168 | # * https://github.com/astral-sh/ruff/issues/10277
169 | at_least_one_million = lambda _: _ >= 1_000_000
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
170 |
171 | x = lambda: (
    |
help: Rewrite `at_least_one_million` as a `def`
    |
168 | # * https://github.com/astral-sh/ruff/issues/10277
    - at_least_one_million = lambda _: _ >= 1_000_000
169 + def at_least_one_million(_):
170 +     return _ >= 1_000_000
171 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:171:1
    |
169 |   at_least_one_million = lambda _: _ >= 1_000_000
170 |
171 | / x = lambda: (
172 | |     # comment
173 | |     5 + 10
174 | | )
    | |_^
175 |
176 |   x = lambda: (
    |
help: Rewrite `x` as a `def`
    |
170 |
    - x = lambda: (
171 + def x():
172 +     return (
173 |     # comment
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:176:1
    |
174 |   )
175 |
176 | / x = lambda: (
177 | |     # comment
178 | |     y := 10
179 | | )
    | |_^
180 |
181 |   # https://github.com/astral-sh/ruff/issues/18475
    |
help: Rewrite `x` as a `def`
    |
175 |
    - x = lambda: (
176 + def x():
177 +     return (
178 |     # comment
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:182:1
    |
181 |   # https://github.com/astral-sh/ruff/issues/18475
182 | / foo_tooltip = (
183 | |     lambda x, data: f"\nfoo: {data['foo'][int(x)]}"
184 | |     if data["foo"] is not None
185 | |     else ""
186 | | )
    | |_^
187 |
188 |   foo_tooltip = (
    |
help: Rewrite `foo_tooltip` as a `def`
    |
181 | # https://github.com/astral-sh/ruff/issues/18475
    - foo_tooltip = (
    -     lambda x, data: f"\nfoo: {data['foo'][int(x)]}"
182 + def foo_tooltip(x, data):
183 +     return (f"\nfoo: {data['foo'][int(x)]}"
184 |     if data["foo"] is not None
    -     else ""
    - )
185 +     else "")
186 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:188:1
    |
186 |   )
187 |
188 | / foo_tooltip = (
189 | |     lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +
190 | |     more
191 | |
192 | | )
    | |_^
193 |
194 |   # https://github.com/astral-sh/ruff/issues/20097
    |
help: Rewrite `foo_tooltip` as a `def`
    |
187 |
    - foo_tooltip = (
    -     lambda x, data: f"\nfoo: {data['foo'][int(x)]}" +
    -     more
    -
    - )
188 + def foo_tooltip(x, data):
189 +     return (f"\nfoo: {data['foo'][int(x)]}" +
190 +     more)
191 |
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:200:5
    |
199 |     P = ParamSpec("P")
200 |     f1: Callable[P, str] = lambda x: str(x)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201 |     f2: Callable[..., str] = lambda x: str(x)
    |
help: Rewrite `f1` as a `def`
    |
199 |     P = ParamSpec("P")
    -     f1: Callable[P, str] = lambda x: str(x)
200 +     def f1(x) -> str:
201 +         return str(x)
202 |     f2: Callable[..., str] = lambda x: str(x)
    |
note: This is an unsafe fix and may change runtime behavior

E731 [*] Do not assign a `lambda` expression, use a `def`
   --> E731.py:201:5
    |
199 |     P = ParamSpec("P")
200 |     f1: Callable[P, str] = lambda x: str(x)
201 |     f2: Callable[..., str] = lambda x: str(x)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
help: Rewrite `f2` as a `def`
    |
200 |     f1: Callable[P, str] = lambda x: str(x)
    -     f2: Callable[..., str] = lambda x: str(x)
201 +     def f2(x) -> str:
202 +         return str(x)
    |
note: This is an unsafe fix and may change runtime behavior
