mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:14:42 +00:00
Avoid attempting to fix invalid Optional
annotations (#7079)
This commit is contained in:
parent
4eaa412370
commit
b70dde4a77
3 changed files with 23 additions and 4 deletions
|
@ -91,3 +91,7 @@ def f(x: Optional[int : float]) -> None:
|
|||
|
||||
def f(x: Optional[str, int : float]) -> None:
|
||||
...
|
||||
|
||||
|
||||
def f(x: Optional[int, float]) -> None:
|
||||
...
|
||||
|
|
|
@ -77,11 +77,18 @@ pub(crate) fn use_pep604_annotation(
|
|||
Pep604Operator::Optional => {
|
||||
let mut diagnostic = Diagnostic::new(NonPEP604Annotation, expr.range());
|
||||
if fixable && checker.patch(diagnostic.kind.rule()) {
|
||||
match slice {
|
||||
Expr::Tuple(_) => {
|
||||
// Invalid type annotation.
|
||||
}
|
||||
_ => {
|
||||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||
optional(slice, checker.locator()),
|
||||
expr.range(),
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
Pep604Operator::Union => {
|
||||
|
|
|
@ -361,4 +361,12 @@ UP007.py:92:10: UP007 Use `X | Y` for type annotations
|
|||
|
|
||||
= help: Convert to `X | Y`
|
||||
|
||||
UP007.py:96:10: UP007 Use `X | Y` for type annotations
|
||||
|
|
||||
96 | def f(x: Optional[int, float]) -> None:
|
||||
| ^^^^^^^^^^^^^^^^^^^^ UP007
|
||||
97 | ...
|
||||
|
|
||||
= help: Convert to `X | Y`
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue