-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recognize all symbols named TYPE_CHECKING
for in_type_checking_block
#15719
Merged
MichaReiser
merged 10 commits into
astral-sh:main
from
Daverball:feat/TYPE_CHECKING_without_typing_import
Feb 6, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f9f6540
Recognize all symbols named `TYPE_CHECKING` for `in_type_checking_block`
Daverball cf42e94
Fixes order of operations in `Importer::typing_import_edit`
Daverball d72a37e
Reduce diff size
MichaReiser fbc98f4
Remove snap file
MichaReiser 6de37c8
Introduces feature flag to gate changes behind preview mode
Daverball c8e2e53
Merge branch 'main' into feat/TYPE_CHECKING_without_typing_import
Daverball 14c8784
`if False` is now allowed to be simplified by SIM108 in preview
Daverball dda1628
Fixes redundant field name warning
Daverball a1501e9
Refactors `typing_import_edit` logic
Daverball 26bf906
Addresses clippy complaint
Daverball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
26 changes: 26 additions & 0 deletions
26
...apshots/ruff_linter__rules__flake8_type_checking__tests__github_issue_15681_fix_test.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,26 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_type_checking/mod.rs | ||
--- | ||
<filename>:4:8: TC003 [*] Move standard library import `pathlib` into a type-checking block | ||
| | ||
2 | from __future__ import annotations | ||
3 | | ||
4 | import pathlib # TC003 | ||
| ^^^^^^^ TC003 | ||
5 | | ||
6 | TYPE_CHECKING = False | ||
| | ||
= help: Move into type-checking block | ||
|
||
ℹ Unsafe fix | ||
1 1 | | ||
2 2 | from __future__ import annotations | ||
3 3 | | ||
4 |-import pathlib # TC003 | ||
5 4 | | ||
6 5 | TYPE_CHECKING = False | ||
7 6 | if TYPE_CHECKING: | ||
7 |+ import pathlib | ||
8 8 | from types import TracebackType | ||
9 9 | | ||
10 10 | def foo(tb: TracebackType) -> pathlib.Path: ... |
4 changes: 4 additions & 0 deletions
4
.../ruff_linter__rules__flake8_type_checking__tests__github_issue_15681_regression_test.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 | ||
--- | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should retain the no-op edit in the most common case (the top-most
TYPE_CHECKING
block is defined in global scope). Although this brought up a question about the implementation ofImporter
:Why are we adding both the if statement and the body AST node to
type_checking_blocks
in global scope but only the body AST node elsewhere? I don't really see an obvious reason. Shouldn't we just always pass the body or always the entire if statement? Or at least either one or the other, but never both. Either way it is pretty confusing and it would seem more consistent to pass in something like a struct that contains the condition, the body and the scope. That would also make it flexible enough to support a non-idiomaticif not TYPE_CHECKING
with anelse
clause.