mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 19:39:58 +00:00
[pyupgrade
] Don't introduce invalid syntax when upgrading old-style type aliases with parenthesized multiline values (UP040
) (#16026)
This commit is contained in:
parent
46fe17767d
commit
efa8a3ddcc
3 changed files with 87 additions and 12 deletions
|
@ -113,3 +113,18 @@ PositiveList = TypeAliasType(
|
||||||
Annotated[T, Gt(0)], # preserved comment
|
Annotated[T, Gt(0)], # preserved comment
|
||||||
], type_params=(T,)
|
], type_params=(T,)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
T: TypeAlias = (
|
||||||
|
int
|
||||||
|
| str
|
||||||
|
)
|
||||||
|
|
||||||
|
T: TypeAlias = ( # comment0
|
||||||
|
# comment1
|
||||||
|
int # comment2
|
||||||
|
# comment3
|
||||||
|
| # comment4
|
||||||
|
# comment5
|
||||||
|
str # comment6
|
||||||
|
# comment7
|
||||||
|
) # comment8
|
||||||
|
|
|
@ -3,9 +3,9 @@ use itertools::Itertools;
|
||||||
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
|
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
|
||||||
use ruff_macros::{derive_message_formats, ViolationMetadata};
|
use ruff_macros::{derive_message_formats, ViolationMetadata};
|
||||||
use ruff_python_ast::name::Name;
|
use ruff_python_ast::name::Name;
|
||||||
use ruff_python_ast::{
|
use ruff_python_ast::parenthesize::parenthesized_range;
|
||||||
visitor::Visitor, Expr, ExprCall, ExprName, Keyword, StmtAnnAssign, StmtAssign,
|
use ruff_python_ast::visitor::Visitor;
|
||||||
};
|
use ruff_python_ast::{Expr, ExprCall, ExprName, Keyword, StmtAnnAssign, StmtAssign, StmtRef};
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
@ -183,8 +183,8 @@ pub(crate) fn non_pep695_type_alias_type(checker: &Checker, stmt: &StmtAssign) {
|
||||||
};
|
};
|
||||||
|
|
||||||
checker.report_diagnostic(create_diagnostic(
|
checker.report_diagnostic(create_diagnostic(
|
||||||
checker.source(),
|
checker,
|
||||||
stmt.range,
|
stmt.into(),
|
||||||
&target_name.id,
|
&target_name.id,
|
||||||
value,
|
value,
|
||||||
&vars,
|
&vars,
|
||||||
|
@ -243,8 +243,8 @@ pub(crate) fn non_pep695_type_alias(checker: &Checker, stmt: &StmtAnnAssign) {
|
||||||
}
|
}
|
||||||
|
|
||||||
checker.report_diagnostic(create_diagnostic(
|
checker.report_diagnostic(create_diagnostic(
|
||||||
checker.source(),
|
checker,
|
||||||
stmt.range(),
|
stmt.into(),
|
||||||
name,
|
name,
|
||||||
value,
|
value,
|
||||||
&vars,
|
&vars,
|
||||||
|
@ -261,26 +261,30 @@ 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.
|
/// Generate a [`Diagnostic`] for a non-PEP 695 type alias or type alias type.
|
||||||
fn create_diagnostic(
|
fn create_diagnostic(
|
||||||
source: &str,
|
checker: &Checker,
|
||||||
stmt_range: TextRange,
|
stmt: StmtRef,
|
||||||
name: &Name,
|
name: &Name,
|
||||||
value: &Expr,
|
value: &Expr,
|
||||||
type_vars: &[TypeVar],
|
type_vars: &[TypeVar],
|
||||||
applicability: Applicability,
|
applicability: Applicability,
|
||||||
type_alias_kind: TypeAliasKind,
|
type_alias_kind: TypeAliasKind,
|
||||||
) -> Diagnostic {
|
) -> 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!(
|
let content = format!(
|
||||||
"type {name}{type_params} = {value}",
|
"type {name}{type_params} = {value}",
|
||||||
type_params = DisplayTypeVars { type_vars, source },
|
type_params = DisplayTypeVars { type_vars, source },
|
||||||
value = &source[value.range()]
|
value = &source[range_with_parentheses]
|
||||||
);
|
);
|
||||||
let edit = Edit::range_replacement(content, stmt_range);
|
let edit = Edit::range_replacement(content, stmt.range());
|
||||||
Diagnostic::new(
|
Diagnostic::new(
|
||||||
NonPEP695TypeAlias {
|
NonPEP695TypeAlias {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
type_alias_kind,
|
type_alias_kind,
|
||||||
},
|
},
|
||||||
stmt_range,
|
stmt.range(),
|
||||||
)
|
)
|
||||||
.with_fix(Fix::applicable_edit(edit, applicability))
|
.with_fix(Fix::applicable_edit(edit, applicability))
|
||||||
}
|
}
|
||||||
|
|
|
@ -417,6 +417,8 @@ UP040.py:111:1: UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignm
|
||||||
114 | | ], type_params=(T,)
|
114 | | ], type_params=(T,)
|
||||||
115 | | )
|
115 | | )
|
||||||
| |_^ UP040
|
| |_^ UP040
|
||||||
|
116 |
|
||||||
|
117 | T: TypeAlias = (
|
||||||
|
|
|
|
||||||
= help: Use the `type` keyword
|
= help: Use the `type` keyword
|
||||||
|
|
||||||
|
@ -431,3 +433,57 @@ UP040.py:111:1: UP040 [*] Type alias `PositiveList` uses `TypeAliasType` assignm
|
||||||
114 |- ], type_params=(T,)
|
114 |- ], type_params=(T,)
|
||||||
115 |-)
|
115 |-)
|
||||||
113 |+ ]
|
113 |+ ]
|
||||||
|
116 114 |
|
||||||
|
117 115 | T: TypeAlias = (
|
||||||
|
118 116 | int
|
||||||
|
|
||||||
|
UP040.py:117:1: UP040 [*] Type alias `T` uses `TypeAlias` annotation instead of the `type` keyword
|
||||||
|
|
|
||||||
|
115 | )
|
||||||
|
116 |
|
||||||
|
117 | / T: TypeAlias = (
|
||||||
|
118 | | int
|
||||||
|
119 | | | str
|
||||||
|
120 | | )
|
||||||
|
| |_^ UP040
|
||||||
|
121 |
|
||||||
|
122 | T: TypeAlias = ( # comment0
|
||||||
|
|
|
||||||
|
= help: Use the `type` keyword
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
114 114 | ], type_params=(T,)
|
||||||
|
115 115 | )
|
||||||
|
116 116 |
|
||||||
|
117 |-T: TypeAlias = (
|
||||||
|
117 |+type T = (
|
||||||
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue