Fix unnecessary parentheses in UP007 fix (#8610)

Fixes #8609
This commit is contained in:
Shantanu 2023-11-10 16:15:09 -08:00 committed by GitHub
parent c8edac9d2b
commit 8207d6df82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -114,3 +114,8 @@ class ServiceRefOrValue:
class Collection(Protocol[*_B0]):
def __iter__(self) -> Iterator[Union[*_B0]]:
...
# Regression test for: https://github.com/astral-sh/ruff/issues/8609
def f(x: Union[int, str, bytes]) -> None:
...

View File

@ -147,10 +147,10 @@ fn union(elts: &[Expr]) -> Expr {
}),
[Expr::Tuple(ast::ExprTuple { elts, .. })] => union(elts),
[elt] => elt.clone(),
[elt, rest @ ..] => Expr::BinOp(ast::ExprBinOp {
left: Box::new(union(&[elt.clone()])),
[rest @ .., elt] => Expr::BinOp(ast::ExprBinOp {
left: Box::new(union(rest)),
op: Operator::BitOr,
right: Box::new(union(rest)),
right: Box::new(union(&[elt.clone()])),
range: TextRange::default(),
}),
}

View File

@ -50,7 +50,7 @@ UP007.py:14:10: UP007 [*] Use `X | Y` for type annotations
12 12 |
13 13 |
14 |-def f(x: Union[str, int, Union[float, bytes]]) -> None:
14 |+def f(x: str | (int | Union[float, bytes])) -> None:
14 |+def f(x: str | int | Union[float, bytes]) -> None:
15 15 | ...
16 16 |
17 17 |
@ -176,7 +176,7 @@ UP007.py:38:11: UP007 [*] Use `X | Y` for type annotations
36 36 |
37 37 |
38 |-def f(x: "Union[str, int, Union[float, bytes]]") -> None:
38 |+def f(x: "str | (int | Union[float, bytes])") -> None:
38 |+def f(x: "str | int | Union[float, bytes]") -> None:
39 39 | ...
40 40 |
41 41 |
@ -414,4 +414,21 @@ UP007.py:110:28: UP007 [*] Use `X | Y` for type annotations
112 112 |
113 113 | # Regression test for: https://github.com/astral-sh/ruff/issues/7452
UP007.py:120:10: UP007 [*] Use `X | Y` for type annotations
|
119 | # Regression test for: https://github.com/astral-sh/ruff/issues/8609
120 | def f(x: Union[int, str, bytes]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^ UP007
121 | ...
|
= help: Convert to `X | Y`
Unsafe fix
117 117 |
118 118 |
119 119 | # Regression test for: https://github.com/astral-sh/ruff/issues/8609
120 |-def f(x: Union[int, str, bytes]) -> None:
120 |+def f(x: int | str | bytes) -> None:
121 121 | ...