Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py
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
Comment on lines +10 to +15

Copy link
Copy Markdown
Member

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).

Copy link
Copy Markdown
Contributor Author

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_definition with in_simple_string_type_definition if we want to filter out the complex cases intentionally.

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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!


# complex (implicitly concatenated) annotations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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"
"]"
)
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
|| checker.target_version() >= PythonVersion::PY310
|| (checker.target_version() >= PythonVersion::PY37
&& checker.semantic.future_annotations_or_stub()
&& checker.semantic.in_annotation()
&& (checker.semantic.in_annotation()
|| checker.semantic.in_string_type_definition())
&& !checker.settings().pyupgrade.keep_runtime_typing)
{
pyupgrade::rules::non_pep604_annotation(checker, expr, slice, operator);
Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_linter/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,17 @@ mod tests {
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}

#[test]
fn up045_future_annotations_py39() -> Result<()> {
let diagnostics = test_path(
Path::new("pyupgrade/UP045_py39.py"),
&settings::LinterSettings {
unresolved_target_version: PythonVersion::PY39.into(),
..settings::LinterSettings::for_rule(Rule::NonPEP604AnnotationOptional)
},
)?;
assert_diagnostics!(diagnostics);
Ok(())
}
}
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