From ab889c0534c9b07b591d1fca2a73f2a1a285528c Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Thu, 30 Jan 2025 17:30:29 -0500 Subject: [PATCH] test preserved comments within value --- .../test/fixtures/pyupgrade/UP040.py | 9 ++++++ .../test/fixtures/pyupgrade/UP040.pyi | 7 +++++ .../rules/pep695/non_pep695_type_alias.rs | 8 +++++- ...er__rules__pyupgrade__tests__UP040.py.snap | 28 +++++++++++++++++++ ...r__rules__pyupgrade__tests__UP040.pyi.snap | 27 +++++++++++++++++- 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py index e7d98c98069ef..c83266d380034 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py @@ -104,3 +104,12 @@ class Foo: PositiveList = TypeAliasType( "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,) ) # this comment should be okay + + +# this comment will actually be preserved because it's inside the "value" part +T = TypeVar("T") +PositiveList = TypeAliasType( + "PositiveList", list[ + Annotated[T, Gt(0)], # preserved comment + ], type_params=(T,) +) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.pyi b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.pyi index e2a18694bd86e..f97ba8887f04d 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.pyi +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.pyi @@ -5,3 +5,10 @@ from typing import TypeAlias # Fixes in type stub files should be safe to apply unlike in regular code where runtime behavior could change x: typing.TypeAlias = int x: TypeAlias = int + + +# comments in the value are preserved +x: TypeAlias = tuple[ + int, # preserved + float, +] diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/pep695/non_pep695_type_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/pep695/non_pep695_type_alias.rs index e44c628494998..430c4752fa443 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/pep695/non_pep695_type_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/pep695/non_pep695_type_alias.rs @@ -152,7 +152,13 @@ pub(crate) fn non_pep695_type_alias_type(checker: &mut Checker, stmt: &StmtAssig return; }; - let safety = if checker.comment_ranges().intersects(stmt.range) { + // it would be easier to check for comments in the whole `stmt.range`, but because + // `create_diagnostic` uses the full source text of `value`, comments within `value` are + // actually preserved. thus, we have to check for comments in `stmt` but outside of `value` + let pre_value = TextRange::new(stmt.start(), value.start()); + let post_value = TextRange::new(value.end(), stmt.end()); + let comment_ranges = checker.comment_ranges(); + let safety = if comment_ranges.intersects(pre_value) || comment_ranges.intersects(post_value) { Applicability::Unsafe } else { Applicability::Safe diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.py.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.py.snap index ad5aca4653da4..9b6ec2d5c99ec 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.py.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.py.snap @@ -403,3 +403,31 @@ UP040.py:104:1: UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignm 105 |- "PositiveList", list[Annotated[T, Gt(0)]], type_params=(T,) 106 |-) # this comment should be okay 104 |+type PositiveList[T] = list[Annotated[T, Gt(0)]] # this comment should be okay +107 105 | +108 106 | +109 107 | # this comment will actually be preserved because it's inside the "value" part + +UP040.py:111:1: UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignment instead of the `type` keyword + | +109 | # this comment will actually be preserved because it's inside the "value" part +110 | T = TypeVar("T") +111 | / PositiveList = TypeAliasType( +112 | | "PositiveList", list[ +113 | | Annotated[T, Gt(0)], # preserved comment +114 | | ], type_params=(T,) +115 | | ) + | |_^ UP040 + | + = help: Use the `type` keyword + +ℹ Safe fix +108 108 | +109 109 | # this comment will actually be preserved because it's inside the "value" part +110 110 | T = TypeVar("T") +111 |-PositiveList = TypeAliasType( +112 |- "PositiveList", list[ + 111 |+type PositiveList[T] = list[ +113 112 | Annotated[T, Gt(0)], # preserved comment +114 |- ], type_params=(T,) +115 |-) + 113 |+ ] diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.pyi.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.pyi.snap index 90329fd870648..30496127fadaa 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.pyi.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP040.pyi.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/pyupgrade/mod.rs -snapshot_kind: text --- UP040.pyi:6:1: UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword | @@ -19,6 +18,8 @@ UP040.pyi:6:1: UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of t 6 |-x: typing.TypeAlias = int 6 |+type x = int 7 7 | x: TypeAlias = int +8 8 | +9 9 | UP040.pyi:7:1: UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword | @@ -35,3 +36,27 @@ UP040.pyi:7:1: UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of t 6 6 | x: typing.TypeAlias = int 7 |-x: TypeAlias = int 7 |+type x = int +8 8 | +9 9 | +10 10 | # comments in the value are preserved + +UP040.pyi:11:1: UP040 [*] Type alias `x` uses `TypeAlias` annotation instead of the `type` keyword + | +10 | # comments in the value are preserved +11 | / x: TypeAlias = tuple[ +12 | | int, # preserved +13 | | float, +14 | | ] + | |_^ UP040 + | + = help: Use the `type` keyword + +ℹ Safe fix +8 8 | +9 9 | +10 10 | # comments in the value are preserved +11 |-x: TypeAlias = tuple[ + 11 |+type x = tuple[ +12 12 | int, # preserved +13 13 | float, +14 14 | ]