Repository navigation
[pyupgrade] Apply UP045 to string arguments of typing.cast
#22320
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ccea64
add regression test
ntBre 29e0897
fix regression test
ntBre dfd0242
test TypeAlias too
ntBre e4602d7
Merge branch 'main' into brent/up045
ntBre 55a3351
add implicitly concatenated annotation tests
ntBre 047818f
Merge branch 'main' into brent/up045
ntBre 7881705
add a todo comment
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
39 changes: 39 additions & 0 deletions
39
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.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,39 @@ | ||
| """ | ||
| Regression test for https://github.com/astral-sh/ruff/issues/20096 | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, cast, TypeAlias | ||
|
|
||
|
|
||
| x: Optional[str] # UP045 | ||
| x: "Optional[str]" # UP045 | ||
| cast("Optional[str]", None) # UP045 | ||
| cast(Optional[str], None) # okay, str | None is a runtime error | ||
| x: TypeAlias = "Optional[str]" # UP045 | ||
| x: TypeAlias = Optional[str] # okay | ||
|
|
||
| # complex (implicitly concatenated) annotations | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a TODO comment here to make it clear, that the rule isn't currently handling implicitly concatenated strings. |
||
| # | ||
| # TODO(brent) these aren't currently flagged by the rule but probably should | ||
| # be. From what I can tell, this isn't an issue in the rule itself but rather | ||
| # with our parsing of such annotations. | ||
| x: ( | ||
| "Optional" | ||
| "[" | ||
| "str" | ||
| "]" | ||
| ) | ||
| cast(( | ||
| "Optional" | ||
| "[" | ||
| "str" | ||
| "]" | ||
| ), None) | ||
| x: TypeAlias = ( | ||
| "Optional" | ||
| "[" | ||
| "str" | ||
| "]" | ||
| ) | ||
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
82 changes: 82 additions & 0 deletions
82
...pgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.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,82 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/pyupgrade/mod.rs | ||
| --- | ||
| UP045 [*] Use `X | None` for type annotations | ||
| --> UP045_py39.py:10:4 | ||
| | | ||
| 10 | x: Optional[str] # UP045 | ||
| | ^^^^^^^^^^^^^ | ||
| 11 | x: "Optional[str]" # UP045 | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| | | ||
| help: Convert to `X | None` | ||
| 7 | from typing import Optional, cast, TypeAlias | ||
| 8 | | ||
| 9 | | ||
| - x: Optional[str] # UP045 | ||
| 10 + x: str | None # UP045 | ||
| 11 | x: "Optional[str]" # UP045 | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| UP045 [*] Use `X | None` for type annotations | ||
| --> UP045_py39.py:11:5 | ||
| | | ||
| 10 | x: Optional[str] # UP045 | ||
| 11 | x: "Optional[str]" # UP045 | ||
| | ^^^^^^^^^^^^^ | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| | | ||
| help: Convert to `X | None` | ||
| 8 | | ||
| 9 | | ||
| 10 | x: Optional[str] # UP045 | ||
| - x: "Optional[str]" # UP045 | ||
| 11 + x: "str | None" # UP045 | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| 14 | x: TypeAlias = "Optional[str]" # UP045 | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| UP045 [*] Use `X | None` for type annotations | ||
| --> UP045_py39.py:12:7 | ||
| | | ||
| 10 | x: Optional[str] # UP045 | ||
| 11 | x: "Optional[str]" # UP045 | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| | ^^^^^^^^^^^^^ | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| 14 | x: TypeAlias = "Optional[str]" # UP045 | ||
| | | ||
| help: Convert to `X | None` | ||
| 9 | | ||
| 10 | x: Optional[str] # UP045 | ||
| 11 | x: "Optional[str]" # UP045 | ||
| - cast("Optional[str]", None) # UP045 | ||
| 12 + cast("str | None", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| 14 | x: TypeAlias = "Optional[str]" # UP045 | ||
| 15 | x: TypeAlias = Optional[str] # okay | ||
| note: This is an unsafe fix and may change runtime behavior | ||
|
|
||
| UP045 [*] Use `X | None` for type annotations | ||
| --> UP045_py39.py:14:17 | ||
| | | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| 14 | x: TypeAlias = "Optional[str]" # UP045 | ||
| | ^^^^^^^^^^^^^ | ||
| 15 | x: TypeAlias = Optional[str] # okay | ||
| | | ||
| help: Convert to `X | None` | ||
| 11 | x: "Optional[str]" # UP045 | ||
| 12 | cast("Optional[str]", None) # UP045 | ||
| 13 | cast(Optional[str], None) # okay, str | None is a runtime error | ||
| - x: TypeAlias = "Optional[str]" # UP045 | ||
| 14 + x: TypeAlias = "str | None" # UP045 | ||
| 15 | x: TypeAlias = Optional[str] # okay | ||
| 16 | | ||
| 17 | # complex (implicitly concatenated) annotations | ||
| note: This is an unsafe fix and may change runtime behavior |
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 we add some tests for "Complex" string annotations (an annotation that uses implicit string concatenation).
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.
Interestingly, the complex cases don't emit diagnostics even with my change. Claude thinks this is a bug, but I'll have to dig into it a bit more.
ty also emits an implicit-concatenated-string-type-annotation diagnostic on the new test cases, so I could possibly just replace
in_string_type_definitionwithin_simple_string_type_definitionif we want to filter out the complex cases intentionally.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.
Yeah, this is a bit surprising. Let me know if you want me to take a closer look
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 I'll just close this for now, unless you think it's worth landing without looking into the (arguably separate) issue with concatenated annotations. This is a pretty niche issue in any case.
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 you have here seems better than what we had before and should cover the majority of stringified type annotations
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.
Sorry, I only just realized that my comment probably made it sound like I had left a bug in the PR. When Claude and I looked into it, the bug appears to be upstream of the rule itself in our complex string annotation parsing, or at least in how that parsing interacts with our semantic model. I think the rule itself is behaving correctly with the annotations it's asked to check.
Thanks for taking another look!