-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use "manual" fixability for E731 in shadowed context (#5430)
## Summary This PR makes E731 a "manual" fix in one other context: when the lambda is shadowing another variable in the scope. Function declarations (with shadowing) cause issues for type checkers, and so rewriting an annotation, e.g., in branches of an `if` statement can lead to failures. Closes #5421.
- Loading branch information
1 parent
72f7f11
commit aa887d5
Showing
3 changed files
with
440 additions
and
306 deletions.
There are no files selected for viewing
160 changes: 122 additions & 38 deletions
160
crates/ruff/resources/test/fixtures/pycodestyle/E731.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,135 @@ | ||
#: E731 | ||
f = lambda x: 2 * x | ||
#: E731 | ||
f = lambda x: 2 * x | ||
#: E731 | ||
while False: | ||
this = lambda y, z: 2 * x | ||
#: E731 | ||
f = lambda: (yield 1) | ||
#: E731 | ||
f = lambda: (yield from g()) | ||
#: E731 | ||
class F: | ||
def scope(): | ||
# E731 | ||
f = lambda x: 2 * x | ||
|
||
|
||
f = object() | ||
f.method = lambda: "Method" | ||
f = {} | ||
f["a"] = lambda x: x**2 | ||
f = [] | ||
f.append(lambda x: x**2) | ||
f = g = lambda x: x**2 | ||
lambda: "no-op" | ||
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 | ||
|
||
# E731 | ||
f: Callable[[int], int] = lambda x: 2 * x | ||
|
||
# Annotated | ||
from typing import Callable, ParamSpec | ||
|
||
P = ParamSpec("P") | ||
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 | ||
|
||
# ParamSpec cannot be used in this context, so do not preserve the annotation. | ||
f: Callable[P, int] = lambda *args: len(args) | ||
f: Callable[[], None] = lambda: None | ||
f: Callable[..., None] = lambda a, b: None | ||
f: Callable[[int], int] = lambda x: 2 * x | ||
|
||
# Let's use the `Callable` type from `collections.abc` instead. | ||
from collections.abc import Callable | ||
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], str] = lambda a, b: a * b | ||
f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b) | ||
f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b] | ||
f: Callable[[str, int], tuple[str, int]] = lambda a, b: (a, b) | ||
|
||
|
||
# Override `Callable` | ||
class Callable: | ||
pass | ||
def scope(): | ||
# E731 | ||
|
||
from collections.abc import Callable | ||
|
||
# Do not copy the annotation from here on out. | ||
f: Callable[[str, int], str] = lambda a, b: a * b | ||
f: Callable[[str, int, list[str]], list[str]] = lambda a, b, /, c: [*c, a * b] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.