Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unnecessary parentheses in UP007 fix #8610

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP007.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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 | ...


Loading