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

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

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

ℹ Suggested fix
1 1 | #: E731
2 2 | f = lambda x: 2 * x
3 3 | #: E731
4   |-f = lambda x: 2 * x
  4 |+def f(x):
  5 |+    return 2 * x
5 6 | #: E731
6 7 | while False:
7 8 |     this = lambda y, z: 2 * x

E731.py:7:5: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
 7 | #: E731
 8 | while False:
 9 |     this = lambda y, z: 2 * x
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ E731
10 | #: E731
11 | f = lambda: (yield 1)
   |
   = help: Rewrite `this` as a `def`

ℹ Suggested fix
4 4 | f = lambda x: 2 * x
5 5 | #: E731
6 6 | while False:
7   |-    this = lambda y, z: 2 * x
  7 |+    def this(y, z):
  8 |+        return 2 * x
8 9 | #: E731
9 10 | f = lambda: (yield 1)
10 11 | #: E731

E731.py:9:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
 9 |     this = lambda y, z: 2 * x
10 | #: E731
11 | f = lambda: (yield 1)
   | ^^^^^^^^^^^^^^^^^^^^^ E731
12 | #: E731
13 | f = lambda: (yield from g())
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
6  6  | while False:
7  7  |     this = lambda y, z: 2 * x
8  8  | #: E731
9     |-f = lambda: (yield 1)
   9  |+def f():
   10 |+    return (yield 1)
10 11 | #: E731
11 12 | f = lambda: (yield from g())
12 13 | #: E731

E731.py:11:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
11 | f = lambda: (yield 1)
12 | #: E731
13 | f = lambda: (yield from g())
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
14 | #: E731
15 | class F:
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
8  8  | #: E731
9  9  | f = lambda: (yield 1)
10 10 | #: E731
11    |-f = lambda: (yield from g())
   11 |+def f():
   12 |+    return (yield from g())
12 13 | #: E731
13 14 | class F:
14 15 |     f = lambda x: 2 * x

E731.py:14:5: E731 Do not assign a `lambda` expression, use a `def`
   |
14 | #: E731
15 | class F:
16 |     f = lambda x: 2 * x
   |     ^^^^^^^^^^^^^^^^^^^ E731
   |

E731.py:32:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
32 | # ParamSpec cannot be used in this context, so do not preserve the annotation.
33 | f: Callable[P, int] = lambda *args: len(args)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
34 | f: Callable[[], None] = lambda: None
35 | f: Callable[..., None] = lambda a, b: None
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
29 29 | P = ParamSpec("P")
30 30 | 
31 31 | # ParamSpec cannot be used in this context, so do not preserve the annotation.
32    |-f: Callable[P, int] = lambda *args: len(args)
   32 |+def f(*args):
   33 |+    return len(args)
33 34 | f: Callable[[], None] = lambda: None
34 35 | f: Callable[..., None] = lambda a, b: None
35 36 | f: Callable[[int], int] = lambda x: 2 * x

E731.py:33:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
33 | # ParamSpec cannot be used in this context, so do not preserve the annotation.
34 | f: Callable[P, int] = lambda *args: len(args)
35 | f: Callable[[], None] = lambda: None
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
36 | f: Callable[..., None] = lambda a, b: None
37 | f: Callable[[int], int] = lambda x: 2 * x
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
30 30 | 
31 31 | # ParamSpec cannot be used in this context, so do not preserve the annotation.
32 32 | f: Callable[P, int] = lambda *args: len(args)
33    |-f: Callable[[], None] = lambda: None
   33 |+def f() -> None:
   34 |+    return None
34 35 | f: Callable[..., None] = lambda a, b: None
35 36 | f: Callable[[int], int] = lambda x: 2 * x
36 37 | 

E731.py:34:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
34 | f: Callable[P, int] = lambda *args: len(args)
35 | f: Callable[[], None] = lambda: None
36 | f: Callable[..., None] = lambda a, b: None
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
37 | f: Callable[[int], int] = lambda x: 2 * x
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
31 31 | # ParamSpec cannot be used in this context, so do not preserve the annotation.
32 32 | f: Callable[P, int] = lambda *args: len(args)
33 33 | f: Callable[[], None] = lambda: None
34    |-f: Callable[..., None] = lambda a, b: None
   34 |+def f(a, b) -> None:
   35 |+    return None
35 36 | f: Callable[[int], int] = lambda x: 2 * x
36 37 | 
37 38 | # Let's use the `Callable` type from `collections.abc` instead.

E731.py:35:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
35 | f: Callable[[], None] = lambda: None
36 | f: Callable[..., None] = lambda a, b: None
37 | f: Callable[[int], int] = lambda x: 2 * x
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
38 | 
39 | # Let's use the `Callable` type from `collections.abc` instead.
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
32 32 | f: Callable[P, int] = lambda *args: len(args)
33 33 | f: Callable[[], None] = lambda: None
34 34 | f: Callable[..., None] = lambda a, b: None
35    |-f: Callable[[int], int] = lambda x: 2 * x
   35 |+def f(x: int) -> int:
   36 |+    return 2 * x
36 37 | 
37 38 | # Let's use the `Callable` type from `collections.abc` instead.
38 39 | from collections.abc import Callable

E731.py:40:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
40 | from collections.abc import Callable
41 | 
42 | f: Callable[[str, int], str] = lambda a, b: a * b
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
43 | f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
44 | f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
37 37 | # Let's use the `Callable` type from `collections.abc` instead.
38 38 | from collections.abc import Callable
39 39 | 
40    |-f: Callable[[str, int], str] = lambda a, b: a * b
   40 |+def f(a: str, b: int) -> str:
   41 |+    return a * b
41 42 | f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
42 43 | f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
43 44 | 

E731.py:41:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
41 | f: Callable[[str, int], str] = lambda a, b: a * b
42 | f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
43 | f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
38 38 | from collections.abc import Callable
39 39 | 
40 40 | f: Callable[[str, int], str] = lambda a, b: a * b
41    |-f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
   41 |+def f(a: str, b: int) -> tuple[str, int]:
   42 |+    return a, b
42 43 | f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
43 44 | 
44 45 | 

E731.py:42:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
42 | f: Callable[[str, int], str] = lambda a, b: a * b
43 | f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
44 | f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
39 39 | 
40 40 | f: Callable[[str, int], str] = lambda a, b: a * b
41 41 | f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b)
42    |-f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b]
   42 |+def f(a: str, b: int, /, c: list[str]) -> list[str]:
   43 |+    return [*c, a * b]
43 44 | 
44 45 | 
45 46 | # Override `Callable`

E731.py:51:1: E731 [*] Do not assign a `lambda` expression, use a `def`
   |
51 | # Do not copy the annotation from here on out.
52 | f: Callable[[str, int], str] = lambda a, b: a * b
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E731
   |
   = help: Rewrite `f` as a `def`

ℹ Suggested fix
48 48 | 
49 49 | 
50 50 | # Do not copy the annotation from here on out.
51    |-f: Callable[[str, int], str] = lambda a, b: a * b
   51 |+def f(a, b):
   52 |+    return a * b


