Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test preserved comments within value
Browse files Browse the repository at this point in the history
ntBre committed Jan 30, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ec87c38 commit ab889c0
Showing 5 changed files with 77 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py
Original file line number Diff line number Diff line change
@@ -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,)
)
Original file line number Diff line number Diff line change
@@ -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,
]
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 |+ ]
Original file line number Diff line number Diff line change
@@ -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 | ]

0 comments on commit ab889c0

Please sign in to comment.