-
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.
[
pyflakes
] Visit forward annotations in TypeAliasType
as types (`…
…F401`) (#15829) ## Summary Fixes #15812 by visiting the second argument as a type definition. ## Test Plan New F401 tests based on the report. --------- Co-authored-by: Alex Waygood <[email protected]>
- Loading branch information
1 parent
4f2aea8
commit fe516e2
Showing
5 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
crates/ruff_linter/resources/test/fixtures/pyflakes/F401_34.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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Regression tests for https://github.com/astral-sh/ruff/issues/15812""" | ||
|
||
|
||
def f(): | ||
from typing import Union | ||
|
||
from typing_extensions import TypeAliasType | ||
|
||
Json = TypeAliasType( | ||
"Json", | ||
"Union[dict[str, Json], list[Json], str, int, float, bool, None]", | ||
) | ||
|
||
|
||
def f(): | ||
from typing import Union | ||
|
||
from typing_extensions import TypeAliasType, TypeVar | ||
|
||
T = TypeVar("T") | ||
V = TypeVar("V") | ||
Json = TypeAliasType( | ||
"Json", | ||
"Union[dict[str, Json], list[Json], str, int, float, bool, T, V, None]", | ||
type_params=(T, V), | ||
) | ||
|
||
|
||
def f(): | ||
from typing import Union | ||
|
||
from typing_extensions import TypeAliasType | ||
|
||
Json = TypeAliasType( | ||
value="Union[dict[str, Json], list[Json], str, int, float, bool, None]", | ||
name="Json", | ||
) | ||
|
||
|
||
# strictly speaking it's a false positive to emit F401 for both of these, but | ||
# we can't really be expected to understand that the strings here are type | ||
# expressions (and type checkers probably wouldn't understand them as type | ||
# expressions either!) | ||
def f(): | ||
from typing import Union | ||
|
||
from typing_extensions import TypeAliasType | ||
|
||
args = [ | ||
"Json", | ||
"Union[dict[str, Json], list[Json], str, int, float, bool, None]", | ||
] | ||
|
||
Json = TypeAliasType(*args) | ||
|
||
|
||
def f(): | ||
from typing import Union | ||
|
||
from typing_extensions import TypeAliasType | ||
|
||
kwargs = { | ||
"name": "Json", | ||
"value": "Union[dict[str, Json], list[Json], str, int, float, bool, None]", | ||
} | ||
|
||
Json = TypeAliasType(**kwargs) |
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
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
41 changes: 41 additions & 0 deletions
41
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F401_F401_34.py.snap
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
--- | ||
F401_34.py:45:24: F401 [*] `typing.Union` imported but unused | ||
| | ||
43 | # expressions either!) | ||
44 | def f(): | ||
45 | from typing import Union | ||
| ^^^^^ F401 | ||
46 | | ||
47 | from typing_extensions import TypeAliasType | ||
| | ||
= help: Remove unused import: `typing.Union` | ||
|
||
ℹ Safe fix | ||
42 42 | # expressions (and type checkers probably wouldn't understand them as type | ||
43 43 | # expressions either!) | ||
44 44 | def f(): | ||
45 |- from typing import Union | ||
46 45 | | ||
47 46 | from typing_extensions import TypeAliasType | ||
48 47 | | ||
|
||
F401_34.py:58:24: F401 [*] `typing.Union` imported but unused | ||
| | ||
57 | def f(): | ||
58 | from typing import Union | ||
| ^^^^^ F401 | ||
59 | | ||
60 | from typing_extensions import TypeAliasType | ||
| | ||
= help: Remove unused import: `typing.Union` | ||
|
||
ℹ Safe fix | ||
55 55 | | ||
56 56 | | ||
57 57 | def f(): | ||
58 |- from typing import Union | ||
59 58 | | ||
60 59 | from typing_extensions import TypeAliasType | ||
61 60 | |
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