-
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
[pyflakes] Avoid false positives in @no_type_check
contexts (F821, F722)
#14615
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9aa4913
add test case from #13824
ntBre 3c5ceab
skip visit_type_definition in a @no_type_check context
ntBre 1b3ae36
add docs to new flag
ntBre 530ddd2
use contains instead of manual check
ntBre 10b2cac
switch to snapshot test
ntBre 3fac5c5
Revert "add test case from #13824"
ntBre 01f29a7
add failing tests for classes where no_type_check is also valid
ntBre 06c1655
move no_type_check check into visit_decorator
ntBre b367082
add and use SemanticModel::in_no_type_check
ntBre 615b4b8
ensure no_type_check propagates into function bodies
ntBre a60b747
make sure typing.no_type_check works too
ntBre 350f6e3
use `match_typing_expr`
ntBre 1d5ee64
ignore `@no_type_check` for classes
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
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
crates/ruff_linter/resources/test/fixtures/pyflakes/F722_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,18 @@ | ||
"""Regression test for #13824. | ||
|
||
Don't report an error when the function being annotated has the | ||
`@no_type_check` decorator. | ||
""" | ||
|
||
from typing import no_type_check | ||
|
||
|
||
@no_type_check | ||
def f(arg: "this isn't python") -> "this isn't python either": | ||
x: "this also isn't python" = 0 | ||
|
||
|
||
@no_type_check | ||
class C: | ||
def f(arg: "this isn't python") -> "this isn't python either": | ||
x: "this also isn't python" = 1 |
18 changes: 18 additions & 0 deletions
18
crates/ruff_linter/resources/test/fixtures/pyflakes/F821_30.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,18 @@ | ||
"""Regression test for #13824. | ||
|
||
Don't report an error when the function being annotated has the | ||
`@no_type_check` decorator. | ||
""" | ||
|
||
from typing import no_type_check | ||
|
||
|
||
@no_type_check | ||
def f(arg: "A") -> "R": | ||
x: "A" = 1 | ||
|
||
|
||
@no_type_check | ||
class C: | ||
def f(self, arg: "B") -> "S": | ||
x: "B" = 1 |
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
5 changes: 5 additions & 0 deletions
5
...ter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F722_F722_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,5 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
snapshot_kind: text | ||
--- | ||
|
5 changes: 5 additions & 0 deletions
5
...er/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F821_F821_30.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,5 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyflakes/mod.rs | ||
snapshot_kind: text | ||
--- | ||
|
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
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.
I think this should use
match_typing_expr
so that it works for@typing.no_type_check
too.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.
So, will this apply to type annotations in the body of the function? And should it, per the spec? (I don't know off-hand.)
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.
Ah thanks, I missed
match_typing_expr
.Yes, this should apply to annotations in the body too, based on my reading of the
no_type_check
docs:Similarly in the typing docs linked in what Micha linked:
I added annotated variables to the test fixtures in my last commit to check this.