-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[flake8-type-checking
] Adds implementation for TC007 and TC008
#12927
Merged
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
3de92a5
Adds initial implementation for TCH008
Daverball e1ca904
Fixes mkdocs and clippy complaints
Daverball 3201588
Fixes runtime forward reference binding lookup
Daverball bc45146
Simulates proper runtime name lookups for more accurate results
Daverball 0df3d12
Adds missing test case back and fixes range overlap check
Daverball 4cb32e1
Fixes docstring
Daverball d9cea8f
Refactors class name book keeping code
Daverball 096eb9d
Removes unnecessary borrow
Daverball e420697
Fixes some logical errors and simplifies class name lookups
Daverball fa9d4df
Guards against TC010/TCH008 overlap
Daverball dd78b52
Adds implementation and tests for TCH007
Daverball 08b7ddc
Fixes TCH004/TCH007 overlap
Daverball bd31311
Merge branch 'main' into feat/tch007-tch008
Daverball b185e4b
Merge branch 'main' into feat/tch007-tch008
Daverball b17c959
Clean up unused import that was accidentally left in
Daverball 643ddb6
Simplifies `quote_type_expression`
Daverball 09986c7
Apply suggestions from code review
Daverball 4044dfa
Re-formats code slightly.
Daverball f51ea65
Merge branch 'main' into feat/tch007-tch008
Daverball 81bb172
Fixes errors and only marks fix as unsafe when there are comments
Daverball 2555f8a
Removes unnecessary `return` statement
Daverball a36bab6
Merge branch 'main' into feat/tch007-tch008
Daverball f2f384e
Merge branch 'main' into feat/tch007-tch008
Daverball 6799411
Fixes new semantic flag values
Daverball 231804b
Merge branch 'main' into feat/tch007-tch008
Daverball bfffb7b
`quote_type_annotation` is once again no longer guaranteed to work
Daverball fd051a8
Addressed some of the feedback from the code review
Daverball 0fc0d4b
Avoids changing the behavior of `TCH004` for now.
Daverball f1ec697
Apply suggestions from code review
Daverball fd8c045
Fixes snapshot and applies a couple of other suggestions
Daverball 410bfe4
Merge branch 'main' into feat/tch007-tch008
Daverball 42c33b2
Remove dummy file that got committed by accident
Daverball f98400a
Improves documentation and naming of type alias related methods/flags
Daverball 4933178
Merge branch 'main' into feat/tch007-tch008
Daverball cce3f00
Adjusts type alias naming to be more distinctive
Daverball 823caaa
Merge branch 'main' into feat/tch007-tch008
Daverball c8e0d99
Fixes error in `ruff.schema.json` introduced by merge
Daverball 536c6a4
Merge branch 'main' into feat/tch007-tch008
Daverball 64c7d30
Extends docstring for `simulate_runtime_load`
Daverball 1bf6315
Fixes cross references in docstring
Daverball fa9c1a4
Merge branch 'main' into feat/tch007-tch008
Daverball 8cf8f27
Address some of the code review comments
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
31 changes: 31 additions & 0 deletions
31
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC007.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,31 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Dict, TypeAlias, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Dict | ||
|
||
from foo import Foo | ||
|
||
OptStr: TypeAlias = str | None | ||
Bar: TypeAlias = Foo[int] | ||
|
||
a: TypeAlias = int # OK | ||
b: TypeAlias = Dict # OK | ||
c: TypeAlias = Foo # TC007 | ||
d: TypeAlias = Foo | None # TC007 | ||
e: TypeAlias = OptStr # TC007 | ||
f: TypeAlias = Bar # TC007 | ||
g: TypeAlias = Foo | Bar # TC007 x2 | ||
h: TypeAlias = Foo[str] # TC007 | ||
i: TypeAlias = (Foo | # TC007 x2 (fix removes comment currently) | ||
Bar) | ||
|
||
type C = Foo # OK | ||
type D = Foo | None # OK | ||
type E = OptStr # OK | ||
type F = Bar # OK | ||
type G = Foo | Bar # OK | ||
type H = Foo[str] # OK | ||
type I = (Foo | # OK | ||
Bar) |
52 changes: 52 additions & 0 deletions
52
crates/ruff_linter/resources/test/fixtures/flake8_type_checking/TC008.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,52 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TypeAlias, TYPE_CHECKING | ||
|
||
from foo import Foo | ||
|
||
if TYPE_CHECKING: | ||
from typing import Dict | ||
|
||
OptStr: TypeAlias = str | None | ||
Bar: TypeAlias = Foo[int] | ||
else: | ||
Bar = Foo | ||
|
||
a: TypeAlias = 'int' # TC008 | ||
b: TypeAlias = 'Dict' # OK | ||
c: TypeAlias = 'Foo' # TC008 | ||
d: TypeAlias = 'Foo[str]' # OK | ||
e: TypeAlias = 'Foo.bar' # OK | ||
f: TypeAlias = 'Foo | None' # TC008 | ||
g: TypeAlias = 'OptStr' # OK | ||
h: TypeAlias = 'Bar' # TC008 | ||
i: TypeAlias = Foo['str'] # TC008 | ||
j: TypeAlias = 'Baz' # OK | ||
k: TypeAlias = 'k | None' # OK | ||
l: TypeAlias = 'int' | None # TC008 (because TC010 is not enabled) | ||
m: TypeAlias = ('int' # TC008 | ||
| None) | ||
n: TypeAlias = ('int' # TC008 (fix removes comment currently) | ||
' | None') | ||
|
||
type B = 'Dict' # TC008 | ||
type D = 'Foo[str]' # TC008 | ||
type E = 'Foo.bar' # TC008 | ||
type G = 'OptStr' # TC008 | ||
type I = Foo['str'] # TC008 | ||
type J = 'Baz' # TC008 | ||
type K = 'K | None' # TC008 | ||
type L = 'int' | None # TC008 (because TC010 is not enabled) | ||
type M = ('int' # TC008 | ||
| None) | ||
type N = ('int' # TC008 (fix removes comment currently) | ||
' | None') | ||
|
||
|
||
class Baz: | ||
a: TypeAlias = 'Baz' # OK | ||
type A = 'Baz' # TC008 | ||
|
||
class Nested: | ||
a: TypeAlias = 'Baz' # OK | ||
type A = 'Baz' # TC008 |
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
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
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.
What's the reason we can't fix this today?
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.
We can, I just didn't want to introduce unrelated changes to this PR, so I opted for a comment as reminder to myself to clean this up in a follow-up pull request.