-
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.
## Summary If you remove `typing` from `exempt-modules`, we tend to panic, since we try to add `TYPE_CHECKING` to `from typing import ...` statements while concurrently attempting to remove other members from that import. This PR adds special-casing for typing imports to avoid such panics. Closes #5331 Closes #9196. Closes #9197.
- Loading branch information
1 parent
29846f5
commit 4b4160e
Showing
10 changed files
with
235 additions
and
35 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/exempt_type_checking_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,7 @@ | ||
"""Add `TYPE_CHECKING` to an existing `typing` import. Another member is moved.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Final | ||
|
||
Const: Final[dict] = {} |
7 changes: 7 additions & 0 deletions
7
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/exempt_type_checking_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,7 @@ | ||
"""Using `TYPE_CHECKING` from an existing `typing` import. Another member is moved.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Final, TYPE_CHECKING | ||
|
||
Const: Final[dict] = {} |
7 changes: 7 additions & 0 deletions
7
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/exempt_type_checking_3.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,7 @@ | ||
"""Using `TYPE_CHECKING` from an existing `typing` import. Another member is moved.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Final, Mapping | ||
|
||
Const: Final[dict] = {} |
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
27 changes: 27 additions & 0 deletions
27
..._type_checking__tests__typing-only-standard-library-import_exempt_type_checking_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,27 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
exempt_type_checking_1.py:5:20: TCH003 [*] Move standard library import `typing.Final` into a type-checking block | ||
| | ||
3 | from __future__ import annotations | ||
4 | | ||
5 | from typing import Final | ||
| ^^^^^ TCH003 | ||
6 | | ||
7 | Const: Final[dict] = {} | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
2 2 | | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 |-from typing import Final | ||
5 |+from typing import TYPE_CHECKING | ||
6 |+ | ||
7 |+if TYPE_CHECKING: | ||
8 |+ from typing import Final | ||
6 9 | | ||
7 10 | Const: Final[dict] = {} | ||
|
||
|
27 changes: 27 additions & 0 deletions
27
..._type_checking__tests__typing-only-standard-library-import_exempt_type_checking_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 | ||
--- | ||
exempt_type_checking_2.py:5:20: TCH003 [*] Move standard library import `typing.Final` into a type-checking block | ||
| | ||
3 | from __future__ import annotations | ||
4 | | ||
5 | from typing import Final, TYPE_CHECKING | ||
| ^^^^^ TCH003 | ||
6 | | ||
7 | Const: Final[dict] = {} | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
2 2 | | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 |-from typing import Final, TYPE_CHECKING | ||
5 |+from typing import TYPE_CHECKING | ||
6 |+ | ||
7 |+if TYPE_CHECKING: | ||
8 |+ from typing import Final | ||
6 9 | | ||
7 10 | Const: Final[dict] = {} | ||
|
||
|
28 changes: 28 additions & 0 deletions
28
..._type_checking__tests__typing-only-standard-library-import_exempt_type_checking_3.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,28 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
exempt_type_checking_3.py:5:20: TCH003 [*] Move standard library import `typing.Final` into a type-checking block | ||
| | ||
3 | from __future__ import annotations | ||
4 | | ||
5 | from typing import Final, Mapping | ||
| ^^^^^ TCH003 | ||
6 | | ||
7 | Const: Final[dict] = {} | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
2 2 | | ||
3 3 | from __future__ import annotations | ||
4 4 | | ||
5 |-from typing import Final, Mapping | ||
5 |+from typing import Mapping | ||
6 |+from typing import TYPE_CHECKING | ||
7 |+ | ||
8 |+if TYPE_CHECKING: | ||
9 |+ from typing import Final | ||
6 10 | | ||
7 11 | Const: Final[dict] = {} | ||
|
||
|
Oops, something went wrong.