Skip to content

Commit

Permalink
Avoid attempting to fix invalid Optional annotations (#7079)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Sep 3, 2023
1 parent 4eaa412 commit b70dde4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP007.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...
15 changes: 11 additions & 4 deletions crates/ruff/src/rules/pyupgrade/rules/use_pep604_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ pub(crate) fn use_pep604_annotation(
Pep604Operator::Optional => {
let mut diagnostic = Diagnostic::new(NonPEP604Annotation, expr.range());
if fixable && checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
optional(slice, checker.locator()),
expr.range(),
)));
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`


0 comments on commit b70dde4

Please sign in to comment.