Skip to content

Commit

Permalink
more tests + use StmtRef
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Feb 7, 2025
1 parent 59dedec commit 06e4503
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP040.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ class Foo:
int
| str
)

T: TypeAlias = ( # comment0
# comment1
int # comment2
# comment3
| # comment4
# comment5
str # comment6
# comment7
) # comment8
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::name::Name;
use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::{AnyNodeRef, Expr, ExprCall, ExprName, Keyword, StmtAnnAssign, StmtAssign};
use ruff_python_ast::{Expr, ExprCall, ExprName, Keyword, StmtAnnAssign, StmtAssign, StmtRef};
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -262,19 +262,21 @@ pub(crate) fn non_pep695_type_alias(checker: &Checker, stmt: &StmtAnnAssign) {
/// Generate a [`Diagnostic`] for a non-PEP 695 type alias or type alias type.
fn create_diagnostic(
checker: &Checker,
stmt: AnyNodeRef,
stmt: StmtRef,
name: &Name,
value: &Expr,
type_vars: &[TypeVar],
applicability: Applicability,
type_alias_kind: TypeAliasKind,
) -> Diagnostic {
let source = checker.source();
let range_with_parentheses =
parenthesized_range(value.into(), stmt.into(), checker.comment_ranges(), source)
.unwrap_or(value.range());
let content = format!(
"type {name}{type_params} = {value}",
type_params = DisplayTypeVars { type_vars, source },
value = &source[parenthesized_range(value.into(), stmt, checker.comment_ranges(), source)
.unwrap_or(value.range())]
value = &source[range_with_parentheses]
);
let edit = Edit::range_replacement(content, stmt.range());
Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ UP040.py:117:1: UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of
119 | | | str
120 | | )
| |_^ UP040
121 |
122 | T: TypeAlias = ( # comment0
|
= help: Use the `type` keyword
Expand All @@ -458,3 +460,30 @@ UP040.py:117:1: UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of
118 118 | int
119 119 | | str
120 120 | )
UP040.py:122:1: UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of the `type` keyword
|
120 | )
121 |
122 | / T: TypeAlias = ( # comment0
123 | | # comment1
124 | | int # comment2
125 | | # comment3
126 | | | # comment4
127 | | # comment5
128 | | str # comment6
129 | | # comment7
130 | | ) # comment8
| |_^ UP040
|
= help: Use the `type` keyword
ℹ Unsafe fix
119 119 | | str
120 120 | )
121 121 |
122 |-T: TypeAlias = ( # comment0
122 |+type T = ( # comment0
123 123 | # comment1
124 124 | int # comment2
125 125 | # comment3

0 comments on commit 06e4503

Please sign in to comment.