-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-pyi] Fix PYI041 not resolving string annotations
#19023
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
Open
robsdedude
wants to merge
5
commits into
astral-sh:main
Choose a base branch
from
robsdedude:fix/pyi041-in-string-annotation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b7d7160
[`flake8_pyi`] Fix `PYI041` not resolving string annotations
robsdedude 18d2b2f
Skip fix offering for concatenated stringinzed annotations
robsdedude e571423
Simplify code
robsdedude 80ce1ef
Merge branch 'main' into fix/pyi041-in-string-annotation
robsdedude 18e90f8
Fix merge (changed reporting format)
robsdedude 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
137 changes: 137 additions & 0 deletions
137
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI041_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,137 @@ | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
| Union, | ||
| ) | ||
|
|
||
| from typing_extensions import ( | ||
| TypeAlias, | ||
| ) | ||
|
|
||
| TA0: TypeAlias = "int" | ||
| TA1: TypeAlias = "int | float | bool" | ||
| TA2: TypeAlias = "Union[int, float, bool]" | ||
|
|
||
|
|
||
| def good1(arg: "int") -> "int | bool": | ||
| ... | ||
|
|
||
|
|
||
| def good2(arg: "int", arg2: "int | bool") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f0(arg1: "float | int") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f1(arg1: "float", *, arg2: "float | list[str] | type[bool] | complex") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f2(arg1: "int", /, arg2: "int | int | float") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f3(arg1: "int", *args: "Union[int | int | float]") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| async def f4(**kwargs: "int | int | float") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f5(arg1: "int", *args: "Union[int, int, float]") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f6(arg1: "int", *args: "Union[Union[int, int, float]]") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f7(arg1: "int", *args: "Union[Union[Union[int, int, float]]]") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f8(arg1: "int", *args: "Union[Union[Union[int | int | float]]]") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| def f9( | ||
| arg: """Union[ # comment | ||
| float, # another | ||
| complex, int]""" | ||
| ) -> "None": | ||
| ... | ||
|
|
||
| def f10( | ||
| arg: """ | ||
| int | # comment | ||
| float | # another | ||
| complex | ||
| """ | ||
| ) -> "None": | ||
| ... | ||
|
|
||
|
|
||
| class Foo: | ||
| def good(self, arg: "int") -> "None": | ||
| ... | ||
|
|
||
| def bad(self, arg: "int | float | complex") -> "None": | ||
| ... | ||
|
|
||
| def bad2(self, arg: "int | Union[float, complex]") -> "None": | ||
| ... | ||
|
|
||
| def bad3(self, arg: "Union[Union[float, complex], int]") -> "None": | ||
| ... | ||
|
|
||
| def bad4(self, arg: "Union[float | complex, int]") -> "None": | ||
| ... | ||
|
|
||
| def bad5(self, arg: "int | (float | complex)") -> "None": | ||
| ... | ||
|
|
||
|
|
||
| # https://github.com/astral-sh/ruff/issues/18298 | ||
| # fix must not yield runtime `None | None | ...` (TypeError) | ||
| class Issue18298: | ||
| def f1(self, arg: "None | int | None | float" = None) -> "None": # PYI041 - no fix | ||
| pass | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
||
| def f2(self, arg: "None | int | None | float" = None) -> "None": ... # PYI041 - with fix | ||
|
|
||
| else: | ||
|
|
||
| def f2(self, arg=None) -> "None": | ||
| pass | ||
|
|
||
| def f3(self, arg: "None | float | None | int | None" = None) -> "None": # PYI041 - with fix | ||
| pass | ||
|
|
||
|
|
||
| class FooStringConcat: | ||
| def good(self, arg: "i" "nt") -> "None": | ||
| ... | ||
|
|
||
| def bad(self, arg: "int " "| float | com" "plex") -> "None": | ||
| ... | ||
|
|
||
| def bad2(self, arg: "int | Union[flo" "at, complex]") -> "None": | ||
| ... | ||
|
|
||
| def bad3(self, arg: "Union[Union[float, com" "plex], int]") -> "None": | ||
| ... | ||
|
|
||
| def bad4(self, arg: "Union[float | complex, in" "t ]") -> "None": | ||
| ... | ||
|
|
||
| def bad5(self, arg: "int | " | ||
| "(float | complex)") -> "None": | ||
| ... | ||
|
|
||
| def bad6(self, arg: "in\ | ||
| t | (float | compl" "ex)") -> "None": | ||
| ... | ||
8 changes: 8 additions & 0 deletions
8
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI041_4.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,8 @@ | ||
| from typing import Union as Uno | ||
|
|
||
|
|
||
| def f1(a: "U" "no[int, fl" "oat, Foo]") -> "None": ... | ||
| def f2(a: "Uno[int, float, Foo]") -> "None": ... | ||
| def f3(a: """Uno[int, float, Foo]""") -> "None": ... | ||
| def f4(a: "Uno[in\ | ||
| t, float, Foo]") -> "None": ... |
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
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
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.
Can you add a test for a type annotation consisting of a concatenated string literal:
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.
Good catch! While detection works, the fix did indeed drop the quotation marks.
Uh oh!
There was an error while loading. Please reload this page.
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 is non-trivial to fix. I'll have to go on a side-mission fixing #19184 first.
My approach was to alter
parse_simple_type_annotation(just likeparse_complex_type_annotationalready does) such that the resulting resolved virtual expression spans that whole string. That way, the rule can just replace the whole string and wrap it in quotes if the original annotation was a forward reference.But alas, that change to
parse_simple_type_annotationamplifies the linked bug to also affect non-concatenated forward refs.Uh oh!
There was an error while loading. Please reload this page.
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 believe this was part of the reason why we decided not to support string annotations in the initial implementation as they aren't as common anymore and we didn't felt like they're worth all the complexity they add.
Could we disable the fix for problematic stringified annotations (I think you can also use triple quoted strings or put them across multiple lines). We should try to add tests for all of those if we decide that supporting them is worth the complexity
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.
Tripple quoted type annotations actually work fine. There are some in the test set.
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 ended up addressing this in a similar way as RUF013 handles is: