def scope(): # E731 f = lambda x: 2 * x def scope(): # E731 f = lambda x: 2 * x def scope(): # E731 while False: this = lambda y, z: 2 * x def scope(): # E731 f = lambda: (yield 1) def scope(): # E731 f = lambda: (yield from g()) def scope(): # OK f = object() f.method = lambda: "Method" def scope(): # OK f = {} f["a"] = lambda x: x**2 def scope(): # OK f = [] f.append(lambda x: x**2) def scope(): # OK f = g = lambda x: x**2 def scope(): # OK lambda: "no-op" class Scope: # E731 f = lambda x: 2 * x class Scope: from typing import Callable # OK f: Callable[[int], int] = lambda x: 2 * x def scope(): # E731 from typing import Callable x: Callable[[int], int] if True: x = lambda: 1 else: x = lambda: 2 return x def scope(): # E731 from typing import Callable, ParamSpec # ParamSpec cannot be used in this context, so do not preserve the annotation. P = ParamSpec("P") f: Callable[P, int] = lambda *args: len(args) def scope(): # E731 from typing import Callable f: Callable[[], None] = lambda: None def scope(): # E731 from typing import Callable f: Callable[..., None] = lambda a, b: None def scope(): # E731 from typing import Callable f: Callable[[int], int] = lambda x: 2 * x # Let's use the `Callable` type from `collections.abc` instead. def scope(): # E731 from collections.abc import Callable f: Callable[[str, int], str] = lambda a, b: a * b def scope(): # E731 from collections.abc import Callable f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b) def scope(): # E731 from collections.abc import Callable f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b] class TemperatureScales(Enum): CELSIUS = (lambda deg_c: deg_c) FAHRENHEIT = (lambda deg_c: deg_c * 9 / 5 + 32) # Regression test for: https://github.com/astral-sh/ruff/issues/7141 def scope(): # E731 f = lambda: ( i := 1, ) from dataclasses import dataclass from typing import Callable @dataclass class FilterDataclass: # OK filter: Callable[[str], bool] = lambda _: True # Regression tests for: # * https://github.com/astral-sh/ruff/issues/7720 x = lambda: """ a b """ # * https://github.com/astral-sh/ruff/issues/10277 at_least_one_million = lambda _: _ >= 1_000_000 x = lambda: ( # comment 5 + 10 ) x = lambda: ( # comment y := 10 )