-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[flake8-type-checking] Fix TC003 false positive with future-annotations
#21125
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b4f51f
[`flake8-type-checking`] Fix `TC003` false positive with `future-anno…
ntBre ba000fc
add failing test
ntBre 7f623e7
fix the test
ntBre cdc848d
add minimized test case from latch ecosystem check
ntBre a561ecb
fix up037 test
ntBre 9af69bf
group RuntimeEvaluated arms
ntBre 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP037_3.py
This file contains hidden or 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,17 @@ | ||
| """ | ||
| Regression test for an ecosystem hit on | ||
| https://github.com/astral-sh/ruff/pull/21125. | ||
|
|
||
| We should mark all of the components of special dataclass annotations as | ||
| runtime-required, not just the first layer. | ||
| """ | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import ClassVar, Optional | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class EmptyCell: | ||
| _singleton: ClassVar[Optional["EmptyCell"]] = None | ||
| # the behavior of _singleton above should match a non-ClassVar | ||
| _doubleton: "EmptyCell" |
This file contains hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
...inter__rules__flake8_type_checking__tests__add_future_import_kw_only__TC003_TC003.py.snap
This file contains hidden or 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 | ||
| --- | ||
| TC003 [*] Move standard library import `os` into a type-checking block | ||
| --> TC003.py:8:12 | ||
| | | ||
| 7 | def f(): | ||
| 8 | import os | ||
| | ^^ | ||
| 9 | | ||
| 10 | x: os | ||
| | | ||
| help: Move into type-checking block | ||
| 2 | | ||
| 3 | For typing-only import detection tests, see `TC002.py`. | ||
| 4 | """ | ||
| 5 + from typing import TYPE_CHECKING | ||
| 6 + | ||
| 7 + if TYPE_CHECKING: | ||
| 8 + import os | ||
| 9 | | ||
| 10 | | ||
| 11 | def f(): | ||
| - import os | ||
| 12 | | ||
| 13 | x: os | ||
| 14 | | ||
| note: This is an unsafe fix and may change runtime behavior |
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
...inter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP037_3.py.snap
This file contains hidden or 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,36 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/pyupgrade/mod.rs | ||
| --- | ||
| UP037 [*] Remove quotes from type annotation | ||
| --> UP037_3.py:15:35 | ||
| | | ||
| 13 | @dataclass(frozen=True) | ||
| 14 | class EmptyCell: | ||
| 15 | _singleton: ClassVar[Optional["EmptyCell"]] = None | ||
| | ^^^^^^^^^^^ | ||
| 16 | # the behavior of _singleton above should match a non-ClassVar | ||
| 17 | _doubleton: "EmptyCell" | ||
| | | ||
| help: Remove quotes | ||
| 12 | | ||
| 13 | @dataclass(frozen=True) | ||
| 14 | class EmptyCell: | ||
| - _singleton: ClassVar[Optional["EmptyCell"]] = None | ||
| 15 + _singleton: ClassVar[Optional[EmptyCell]] = None | ||
| 16 | # the behavior of _singleton above should match a non-ClassVar | ||
| 17 | _doubleton: "EmptyCell" | ||
|
|
||
| UP037 [*] Remove quotes from type annotation | ||
| --> UP037_3.py:17:17 | ||
| | | ||
| 15 | _singleton: ClassVar[Optional["EmptyCell"]] = None | ||
| 16 | # the behavior of _singleton above should match a non-ClassVar | ||
| 17 | _doubleton: "EmptyCell" | ||
| | ^^^^^^^^^^^ | ||
| | | ||
| help: Remove quotes | ||
| 14 | class EmptyCell: | ||
| 15 | _singleton: ClassVar[Optional["EmptyCell"]] = None | ||
| 16 | # the behavior of _singleton above should match a non-ClassVar | ||
| - _doubleton: "EmptyCell" | ||
| 17 + _doubleton: EmptyCell |
4 changes: 4 additions & 0 deletions
4
...es/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__rules_py313__UP037_3.py.snap
This file contains hidden or 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/pyupgrade/mod.rs | ||
| --- | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.