-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve detection of
TYPE_CHECKING
blocks imported from `typing_ext…
…ensions` or `_typeshed` (#8429) ~Improves detection of types imported from `typing_extensions`. Removes the hard-coded list of supported types in `typing_extensions`; instead assuming all types could be imported from `typing`, `_typeshed`, or `typing_extensions`.~ ~The typing extensions package appears to re-export types even if they do not need modification.~ Adds detection of `if typing_extensions.TYPE_CHECKING` blocks. Avoids inserting a new `if TYPE_CHECKING` block and `from typing import TYPE_CHECKING` if `typing_extensions.TYPE_CHECKING` is used (closes #8427) --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
- Loading branch information
1 parent
9d167a1
commit 565ddeb
Showing
16 changed files
with
194 additions
and
9 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH004_16.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,10 @@ | ||
from __future__ import annotations | ||
|
||
from typing_extensions import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from pandas import DataFrame | ||
|
||
|
||
def example() -> DataFrame: | ||
pass |
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TCH004_17.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,10 @@ | ||
from __future__ import annotations | ||
|
||
from typing_extensions import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from pandas import DataFrame | ||
|
||
|
||
def example() -> DataFrame: | ||
x = DataFrame() |
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 |
---|---|---|
|
@@ -37,3 +37,9 @@ class Test: | |
|
||
if 0: | ||
x: List | ||
|
||
|
||
from typing_extensions import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
pass # TCH005 |
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/typing_modules_1.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,9 @@ | ||
from __future__ import annotations | ||
|
||
from typing_extensions import Self | ||
|
||
|
||
def func(): | ||
from pandas import DataFrame | ||
|
||
df: DataFrame |
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/typing_modules_2.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,9 @@ | ||
from __future__ import annotations | ||
|
||
import typing_extensions | ||
|
||
|
||
def func(): | ||
from pandas import DataFrame | ||
|
||
df: DataFrame |
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
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
4 changes: 4 additions & 0 deletions
4
...les__flake8_type_checking__tests__runtime-import-in-type-checking-block_TCH004_16.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,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
|
25 changes: 25 additions & 0 deletions
25
...les__flake8_type_checking__tests__runtime-import-in-type-checking-block_TCH004_17.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,25 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
TCH004_17.py:6:24: TCH004 [*] Move import `pandas.DataFrame` out of type-checking block. Import is used for more than type hinting. | ||
| | ||
5 | if TYPE_CHECKING: | ||
6 | from pandas import DataFrame | ||
| ^^^^^^^^^ TCH004 | ||
| | ||
= help: Move out of type-checking block | ||
|
||
ℹ Unsafe fix | ||
1 1 | from __future__ import annotations | ||
2 2 | | ||
3 3 | from typing_extensions import TYPE_CHECKING | ||
4 |+from pandas import DataFrame | ||
4 5 | | ||
5 6 | if TYPE_CHECKING: | ||
6 |- from pandas import DataFrame | ||
7 |+ pass | ||
7 8 | | ||
8 9 | | ||
9 10 | def example() -> DataFrame: | ||
|
||
|
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
29 changes: 29 additions & 0 deletions
29
...les__flake8_type_checking__tests__typing-only-third-party-import_typing_modules_1.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,29 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
typing_modules_1.py:7:24: TCH002 [*] Move third-party import `pandas.DataFrame` into a type-checking block | ||
| | ||
6 | def func(): | ||
7 | from pandas import DataFrame | ||
| ^^^^^^^^^ TCH002 | ||
8 | | ||
9 | df: DataFrame | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
1 1 | from __future__ import annotations | ||
2 2 | | ||
3 3 | from typing_extensions import Self | ||
4 |+from typing import TYPE_CHECKING | ||
5 |+ | ||
6 |+if TYPE_CHECKING: | ||
7 |+ from pandas import DataFrame | ||
4 8 | | ||
5 9 | | ||
6 10 | def func(): | ||
7 |- from pandas import DataFrame | ||
8 11 | | ||
9 12 | df: DataFrame | ||
|
||
|
27 changes: 27 additions & 0 deletions
27
...les__flake8_type_checking__tests__typing-only-third-party-import_typing_modules_2.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,27 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
typing_modules_2.py:7:24: TCH002 [*] Move third-party import `pandas.DataFrame` into a type-checking block | ||
| | ||
6 | def func(): | ||
7 | from pandas import DataFrame | ||
| ^^^^^^^^^ TCH002 | ||
8 | | ||
9 | df: DataFrame | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
2 2 | | ||
3 3 | import typing_extensions | ||
4 4 | | ||
5 |+if typing_extensions.TYPE_CHECKING: | ||
6 |+ from pandas import DataFrame | ||
7 |+ | ||
5 8 | | ||
6 9 | def func(): | ||
7 |- from pandas import DataFrame | ||
8 10 | | ||
9 11 | df: DataFrame | ||
|
||
|
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