Avoid attempting to fix invalid Optional annotations (#7079)

This commit is contained in:
Charlie Marsh 2023-09-03 14:23:36 +01:00 committed by GitHub
parent 4eaa412370
commit b70dde4a77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View file

@ -91,3 +91,7 @@ def f(x: Optional[int : float]) -> None:
def f(x: Optional[str, int : float]) -> None: def f(x: Optional[str, int : float]) -> None:
... ...
def f(x: Optional[int, float]) -> None:
...

View file

@ -77,11 +77,18 @@ pub(crate) fn use_pep604_annotation(
Pep604Operator::Optional => { Pep604Operator::Optional => {
let mut diagnostic = Diagnostic::new(NonPEP604Annotation, expr.range()); let mut diagnostic = Diagnostic::new(NonPEP604Annotation, expr.range());
if fixable && checker.patch(diagnostic.kind.rule()) { if fixable && checker.patch(diagnostic.kind.rule()) {
match slice {
Expr::Tuple(_) => {
// Invalid type annotation.
}
_ => {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
optional(slice, checker.locator()), optional(slice, checker.locator()),
expr.range(), expr.range(),
))); )));
} }
}
}
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }
Pep604Operator::Union => { Pep604Operator::Union => {

View file

@ -361,4 +361,12 @@ UP007.py:92:10: UP007 Use `X | Y` for type annotations
| |
= help: Convert to `X | Y` = 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`