---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E731.py:3:5: E731 [*] Do not assign a `lambda` expression, use a `def`
  |
1 | def scope():
2 |     # E731
3 |     f = lambda x: 2 * x
  |     ^^^^^^^^^^^^^^^^^^^ E731
  |
  = help: Rewrite `f` as a `def`

ℹ Unsafe fix
1 1 | def scope():
2 2 |     # E731
3   |-    f = lambda x: 2 * x
  3 |+    def f(x):
  4 |+        return 2 * x
4 5 | 
5 6 | 
6 7 | def scope():

E731.py:8:5: E731 [*] Do not assign a `lambda` expression, use a `def`
  |
6 | def scope():
7 |     # E731
8 |     f = lambda x: 2 * x
  |     ^^^^^^^^^^^^^^^^^^^ E731
  |
  = help: Rewrite `f` as a `def`

ℹ Unsafe fix
5  5  | 
6  6  | def scope():
7  7  |     # E731
8     |-    f = lambda x: 2 * x
   8  |+    def f(x):
   9  |+        return 2 * x
9  10 | 
10 11 | 
11 12 | def scope():

E731.py:14:9: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
12 |     # E731
13 |     while False:
14 |         this = lambda y, z: 2 * x
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `this` as a `def`

ℹ Unsafe fix
11 11 | def scope():
12 12 |     # E731
13 13 |     while False:
14    |-        this = lambda y, z: 2 * x
   14 |+        def this(y, z):
   15 |+            return 2 * x
15 16 | 
16 17 | 
17 18 | def scope():

E731.py:19:5: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
17 | def scope():
18 |     # E731
19 |     f = lambda: (yield 1)
   |     ^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Unsafe fix
16 16 | 
17 17 | def scope():
18 18 |     # E731
19    |-    f = lambda: (yield 1)
   19 |+    def f():
   20 |+        return (yield 1)
20 21 | 
21 22 | 
22 23 | def scope():

E731.py:24:5: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
22 | def scope():
23 |     # E731
24 |     f = lambda: (yield from g())
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Unsafe fix
21 21 | 
22 22 | def scope():
23 23 |     # E731
24    |-    f = lambda: (yield from g())
   24 |+    def f():
   25 |+        return (yield from g())
25 26 | 
26 27 | 
27 28 | def scope():

E731.py:57:5: E731 Do not assign a `lambda` expression, use a `def`
   |
55 | class Scope:
56 |     # E731
57 |     f = lambda x: 2 * x
   |     ^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Display-only fix
54 54 | 
55 55 | class Scope:
56 56 |     # E731
57    |-    f = lambda x: 2 * x
   57 |+    def f(x):
   58 |+        return 2 * x
58 59 | 
59 60 | 
60 61 | class Scope:

E731.py:73:9: E731 Do not assign a `lambda` expression, use a `def`
   |
71 |     x: Callable[[int], int]
72 |     if True:
73 |         x = lambda: 1
   |         ^^^^^^^^^^^^^ E731
74 |     else:
75 |         x = lambda: 2
   |
   = help: Rewrite `x` as a `def`

ℹ Display-only fix
70 70 | 
71 71 |     x: Callable[[int], int]
72 72 |     if True:
73    |-        x = lambda: 1
   73 |+        def x():
   74 |+            return 1
74 75 |     else:
75 76 |         x = lambda: 2
76 77 |     return x

E731.py:75:9: E731 Do not assign a `lambda` expression, use a `def`
   |
73 |         x = lambda: 1
74 |     else:
75 |         x = lambda: 2
   |         ^^^^^^^^^^^^^ E731
76 |     return x
   |
   = help: Rewrite `x` as a `def`

ℹ Display-only fix
72 72 |     if True:
73 73 |         x = lambda: 1
74 74 |     else:
75    |-        x = lambda: 2
   75 |+        def x():
   76 |+            return 2
76 77 |     return x
77 78 | 
78 79 | 

E731.py:86:5: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
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)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Unsafe fix
83 83 | 
84 84 |     # ParamSpec cannot be used in this context, so do not preserve the annotation.
85 85 |     P = ParamSpec("P")
86    |-    f: Callable[P, int] = lambda *args: len(args)
   86 |+    def f(*args):
   87 |+        return len(args)
87 88 | 
88 89 | 
89 90 | def scope():

E731.py:94:5: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
92 |     from typing import Callable
93 |
94 |     f: Callable[[], None] = lambda: None
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Unsafe fix
91 91 | 
92 92 |     from typing import Callable
93 93 | 
94    |-    f: Callable[[], None] = lambda: None
   94 |+    def f() -> None:
   95 |+        return None
95 96 | 
96 97 | 
97 98 | def scope():

E731.py:102:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
100 |     from typing import Callable
101 |
102 |     f: Callable[..., None] = lambda a, b: None
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
99  99  | 
100 100 |     from typing import Callable
101 101 | 
102     |-    f: Callable[..., None] = lambda a, b: None
    102 |+    def f(a, b) -> None:
    103 |+        return None
103 104 | 
104 105 | 
105 106 | def scope():

E731.py:110:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
108 |     from typing import Callable
109 |
110 |     f: Callable[[int], int] = lambda x: 2 * x
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
107 107 | 
108 108 |     from typing import Callable
109 109 | 
110     |-    f: Callable[[int], int] = lambda x: 2 * x
    110 |+    def f(x: int) -> int:
    111 |+        return 2 * x
111 112 | 
112 113 | 
113 114 | # Let's use the `Callable` type from `collections.abc` instead.

E731.py:119:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
117 |     from collections.abc import Callable
118 |
119 |     f: Callable[[str, int], str] = lambda a, b: a * b
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
116 116 | 
117 117 |     from collections.abc import Callable
118 118 | 
119     |-    f: Callable[[str, int], str] = lambda a, b: a * b
    119 |+    def f(a: str, b: int) -> str:
    120 |+        return a * b
120 121 | 
121 122 | 
122 123 | def scope():

E731.py:127:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
125 |     from collections.abc import Callable
126 |
127 |     f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
124 124 | 
125 125 |     from collections.abc import Callable
126 126 | 
127     |-    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)
128 129 | 
129 130 | 
130 131 | def scope():

E731.py:135:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
133 |     from collections.abc import Callable
134 |
135 |     f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
132 132 | 
133 133 |     from collections.abc import Callable
134 134 | 
135     |-    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]
136 137 | 
137 138 | 
138 139 | class TemperatureScales(Enum):

E731.py:139:5: E731 Do not assign a `lambda` expression, use a `def`
    |
138 | class TemperatureScales(Enum):
139 |     CELSIUS = (lambda deg_c: deg_c)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
140 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    |
    = help: Rewrite `CELSIUS` as a `def`

ℹ Display-only fix
136 136 | 
137 137 | 
138 138 | class TemperatureScales(Enum):
139     |-    CELSIUS = (lambda deg_c: deg_c)
    139 |+    def CELSIUS(deg_c):
    140 |+        return deg_c
140 141 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
141 142 | 
142 143 | 

E731.py:140:5: E731 Do not assign a `lambda` expression, use a `def`
    |
138 | class TemperatureScales(Enum):
139 |     CELSIUS = (lambda deg_c: deg_c)
140 |     FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
    |
    = help: Rewrite `FAHRENHEIT` as a `def`

ℹ Display-only fix
137 137 | 
138 138 | class TemperatureScales(Enum):
139 139 |     CELSIUS = (lambda deg_c: deg_c)
140     |-    FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32)
    140 |+    def FAHRENHEIT(deg_c):
    141 |+        return deg_c * 9 / 5 + 32
141 142 | 
142 143 | 
143 144 | # Regression test for: https://github.com/astral-sh/ruff/issues/7141

E731.py:147:5: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
145 |       # E731
146 |
147 | /     f = lambda: (
148 | |         i := 1,
149 | |     )
    | |_____^ E731
    |
    = help: Rewrite `f` as a `def`

ℹ Unsafe fix
144 144 | def scope():
145 145 |     # E731
146 146 | 
147     |-    f = lambda: (
148     |-        i := 1,
149     |-    )
    147 |+    def f():
    148 |+        return (
    149 |+            i := 1,
    150 |+        )
150 151 | 
151 152 | 
152 153 | from dataclasses import dataclass

E731.py:163:1: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
161 |   # Regression tests for:
162 |   # * https://github.com/astral-sh/ruff/issues/7720
163 | / x = lambda: """
164 | |         a
165 | |     b
166 | | """
    | |___^ E731
167 |
168 |   # * https://github.com/astral-sh/ruff/issues/10277
    |
    = help: Rewrite `x` as a `def`

ℹ Unsafe fix
160 160 | 
161 161 | # Regression tests for:
162 162 | # * https://github.com/astral-sh/ruff/issues/7720
163     |-x = lambda: """
    163 |+def x():
    164 |+    return """
164 165 |         a
165 166 |     b
166 167 | """

E731.py:169:1: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
168 | # * https://github.com/astral-sh/ruff/issues/10277
169 | at_least_one_million = lambda _: _ >= 1_000_000
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
170 |
171 | x = lambda: (
    |
    = help: Rewrite `at_least_one_million` as a `def`

ℹ Unsafe fix
166 166 | """
167 167 | 
168 168 | # * https://github.com/astral-sh/ruff/issues/10277
169     |-at_least_one_million = lambda _: _ >= 1_000_000
    169 |+def at_least_one_million(_):
    170 |+    return _ >= 1_000_000
170 171 | 
171 172 | x = lambda: (
172 173 |     # comment

E731.py:171:1: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
169 |   at_least_one_million = lambda _: _ >= 1_000_000
170 |
171 | / x = lambda: (
172 | |     # comment
173 | |     5 + 10
174 | | )
    | |_^ E731
175 |
176 |   x = lambda: (
    |
    = help: Rewrite `x` as a `def`

ℹ Unsafe fix
168 168 | # * https://github.com/astral-sh/ruff/issues/10277
169 169 | at_least_one_million = lambda _: _ >= 1_000_000
170 170 | 
171     |-x = lambda: (
    171 |+def x():
    172 |+    return (
172 173 |     # comment
173 174 |     5 + 10
174 175 | )

E731.py:176:1: E731 [*] Do not assign a `lambda` expression, use a `def`
    |
174 |   )
175 |
176 | / x = lambda: (
177 | |     # comment
178 | |     y := 10
179 | | )
    | |_^ E731
    |
    = help: Rewrite `x` as a `def`

ℹ Unsafe fix
173 173 |     5 + 10
174 174 | )
175 175 | 
176     |-x = lambda: (
    176 |+def x():
    177 |+    return (
177 178 |     # comment
178 179 |     y := 10
179 180 | )
