From 8ccea64ed4b615d01cfe1daedad934e34f01060b Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 31 Dec 2025 16:59:12 -0500 Subject: [PATCH 1/5] add regression test --- .../test/fixtures/pyupgrade/UP045_py39.py | 12 ++++++ crates/ruff_linter/src/rules/pyupgrade/mod.rs | 13 +++++++ ..._tests__up045_future_annotations_py39.snap | 37 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py create mode 100644 crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py new file mode 100644 index 0000000000000..357f0b170cbe2 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py @@ -0,0 +1,12 @@ +""" +Regression test for https://github.com/astral-sh/ruff/issues/20096 +""" + +from __future__ import annotations + +from typing import Optional, cast + + +x: Optional[str] # UP045 +x: "Optional[str]" # UP045 +cast("Optional[str]", None) # UP045 diff --git a/crates/ruff_linter/src/rules/pyupgrade/mod.rs b/crates/ruff_linter/src/rules/pyupgrade/mod.rs index a4bd1c5ac66bc..0a42032f6da54 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/mod.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/mod.rs @@ -452,4 +452,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(()) + } } diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap new file mode 100644 index 0000000000000..0ea13ca754cb0 --- /dev/null +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap @@ -0,0 +1,37 @@ +--- +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 +8 | +9 | + - x: Optional[str] # UP045 +10 + x: str | None # UP045 +11 | x: "Optional[str]" # UP045 +12 | cast("Optional[str]", None) # UP045 +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 + | +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 +note: This is an unsafe fix and may change runtime behavior From 29e089781d47e6e3d865dfcfcef542daacdd9766 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 31 Dec 2025 17:02:40 -0500 Subject: [PATCH 2/5] fix regression test --- .../test/fixtures/pyupgrade/UP045_py39.py | 1 + .../src/checkers/ast/analyze/expression.rs | 3 ++- ..._tests__up045_future_annotations_py39.snap | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py index 357f0b170cbe2..3a5aab983d51d 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py @@ -10,3 +10,4 @@ x: Optional[str] # UP045 x: "Optional[str]" # UP045 cast("Optional[str]", None) # UP045 +cast(Optional[str], None) # okay, str | None is a runtime error diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index b066678ff4d1f..7791d7160722d 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -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); diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap index 0ea13ca754cb0..d2270601ee88d 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap @@ -17,6 +17,7 @@ help: Convert to `X | None` 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 @@ -26,6 +27,7 @@ UP045 [*] Use `X | None` for type annotations 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 | @@ -34,4 +36,23 @@ help: Convert to `X | None` - 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 +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 + | +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 note: This is an unsafe fix and may change runtime behavior From dfd02426a29f85c66d7fce30daad1ead3c248581 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 31 Dec 2025 17:35:52 -0500 Subject: [PATCH 3/5] test TypeAlias too --- .../test/fixtures/pyupgrade/UP045_py39.py | 4 +++- ..._tests__up045_future_annotations_py39.snap | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py index 3a5aab983d51d..e1af45d99c0d4 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py @@ -4,10 +4,12 @@ from __future__ import annotations -from typing import Optional, cast +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 diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap index d2270601ee88d..b109a463aedf2 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap @@ -10,7 +10,7 @@ UP045 [*] Use `X | None` for type annotations 12 | cast("Optional[str]", None) # UP045 | help: Convert to `X | None` -7 | from typing import Optional, cast +7 | from typing import Optional, cast, TypeAlias 8 | 9 | - x: Optional[str] # UP045 @@ -37,6 +37,7 @@ help: Convert to `X | None` 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 @@ -47,6 +48,7 @@ UP045 [*] Use `X | None` for type annotations 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 | @@ -55,4 +57,24 @@ help: Convert to `X | None` - 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 note: This is an unsafe fix and may change runtime behavior From 55a335165ab126d50edb89e775c0c5c340cb8ae3 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Mon, 12 Jan 2026 17:54:41 -0500 Subject: [PATCH 4/5] add implicitly concatenated annotation tests --- .../test/fixtures/pyupgrade/UP045_py39.py | 20 +++++++++++++++++++ ..._tests__up045_future_annotations_py39.snap | 2 ++ 2 files changed, 22 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py index e1af45d99c0d4..9ed839389a681 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py @@ -13,3 +13,23 @@ 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 +x: ( + "Optional" + "[" + "str" + "]" +) +cast(( + "Optional" + "[" + "str" + "]" +), None) +x: TypeAlias = ( + "Optional" + "[" + "str" + "]" +) diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap index b109a463aedf2..91290d17c20a1 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__up045_future_annotations_py39.snap @@ -77,4 +77,6 @@ help: Convert to `X | None` - 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 From 7881705dd25ec4a27e661f0e5fc049377c616c04 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Thu, 22 Jan 2026 09:15:33 -0500 Subject: [PATCH 5/5] add a todo comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm not sure how accurate this is, but it sounds fairly plausible and potentially worth recording. This is what Claude said when asked to diagnose the issue: > Simple strings use parse_string_annotation → parse_expression_range which: > - Takes the full source code > - Properly offsets each parsed node to be at the correct position within the source file > - Results in unique, accurate ranges for each node (e.g., 200..208 for Optional, 209..212 for str) > > Complex strings use parse_complex_type_annotation which: > - Parses just the concatenated string content: string_expr.value.to_str() (just "Optional[str]" without source context) > - Calls relocate_expr(parsed.expr_mut(), string_expr.range()) > - relocate_expr sets ALL nodes to the SAME range (the entire concatenated expression range) > - Results in every node having identical ranges: 472..508 for everything > > This is indeed a bug. The relocate_expr approach breaks symbol resolution because: > > 1. The semantic model likely uses ranges to look up bindings > 2. When all nodes have the same range, it can't differentiate between Optional and str > 3. This causes resolve_qualified_name to fail, returning ["", "Optional"] instead of ["typing", "Optional"] > > The comment in the code says complex strings "cannot be accurately located" - but that should only prevent fixes from being applied, not prevent diagnostics from being emitted. Both simple and complex annotations have access to the same imports. so I think it's pointing to the `relocate_expr` call in `parse_complex_type_annotation` in `crates/ruff_python_parser/src/typing.rs`. I'm also not sure that we can do much better when parsing the annotation, but we may be able to work around this later in the process anyway. --- .../resources/test/fixtures/pyupgrade/UP045_py39.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py index 9ed839389a681..346fc4a65a4ce 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP045_py39.py @@ -15,6 +15,10 @@ x: TypeAlias = Optional[str] # okay # complex (implicitly concatenated) annotations +# +# 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" "["