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

Handle single element cases in PYI016 #11273

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ def func2() -> str | str: # PYI016: Duplicate union member `str`
# duplicates of the outer `int`), but not three times (which would indicate that
# we incorrectly re-checked the nested union).
field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`

# Single element unions should also be accounted for
field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ field24: typing.Union[int, typing.Union[int, int]] # PYI016: Duplicate union me
# duplicates of the outer `int`), but not three times (which would indicate that
# we incorrectly re-checked the nested union).
field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`

# Single element unions should also be accounted for
field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ PYI016.py:86:28: PYI016 [*] Duplicate union member `int`
85 | # we incorrectly re-checked the nested union).
86 | field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
| ^^^ PYI016
87 |
88 | # Single element unions should also be accounted for
|
= help: Remove duplicate union member `int`

Expand All @@ -523,13 +525,18 @@ PYI016.py:86:28: PYI016 [*] Duplicate union member `int`
85 85 | # we incorrectly re-checked the nested union).
86 |-field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
86 |+field25: typing.Union[int, int] # PYI016: Duplicate union member `int`
87 87 |
88 88 | # Single element unions should also be accounted for
89 89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`

PYI016.py:86:34: PYI016 [*] Duplicate union member `int`
|
84 | # duplicates of the outer `int`), but not three times (which would indicate that
85 | # we incorrectly re-checked the nested union).
86 | field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
| ^^^ PYI016
87 |
88 | # Single element unions should also be accounted for
|
= help: Remove duplicate union member `int`

Expand All @@ -539,5 +546,14 @@ PYI016.py:86:34: PYI016 [*] Duplicate union member `int`
85 85 | # we incorrectly re-checked the nested union).
86 |-field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
86 |+field25: typing.Union[int, int] # PYI016: Duplicate union member `int`
87 87 |
88 88 | # Single element unions should also be accounted for
89 89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`


PYI016.py:89:41: PYI016 Duplicate union member `int`
|
88 | # Single element unions should also be accounted for
89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`
| ^^^ PYI016
|
= help: Remove duplicate union member `int`
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ PYI016.pyi:86:28: PYI016 [*] Duplicate union member `int`
85 | # we incorrectly re-checked the nested union).
86 | field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
| ^^^ PYI016
87 |
88 | # Single element unions should also be accounted for
|
= help: Remove duplicate union member `int`

Expand All @@ -523,13 +525,18 @@ PYI016.pyi:86:28: PYI016 [*] Duplicate union member `int`
85 85 | # we incorrectly re-checked the nested union).
86 |-field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
86 |+field25: typing.Union[int, int] # PYI016: Duplicate union member `int`
87 87 |
88 88 | # Single element unions should also be accounted for
89 89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`

PYI016.pyi:86:34: PYI016 [*] Duplicate union member `int`
|
84 | # duplicates of the outer `int`), but not three times (which would indicate that
85 | # we incorrectly re-checked the nested union).
86 | field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
| ^^^ PYI016
87 |
88 | # Single element unions should also be accounted for
|
= help: Remove duplicate union member `int`

Expand All @@ -539,5 +546,14 @@ PYI016.pyi:86:34: PYI016 [*] Duplicate union member `int`
85 85 | # we incorrectly re-checked the nested union).
86 |-field25: typing.Union[int, int | int] # PYI016: Duplicate union member `int`
86 |+field25: typing.Union[int, int] # PYI016: Duplicate union member `int`
87 87 |
88 88 | # Single element unions should also be accounted for
89 89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`


PYI016.pyi:89:41: PYI016 Duplicate union member `int`
|
88 | # Single element unions should also be accounted for
89 | field26: typing.Union[int, typing.Union[int]] # PYI016: Duplicate union member `int`
| ^^^ PYI016
|
= help: Remove duplicate union member `int`
17 changes: 11 additions & 6 deletions crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,17 @@ where
// Ex) Union[x, y]
if let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = expr {
if semantic.match_typing_expr(value, "Union") {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
// Traverse each element of the tuple within the union recursively to handle cases
// such as `Union[..., Union[...]]
elts.iter()
.for_each(|elt| inner(func, semantic, elt, Some(expr)));
return;
match slice.as_ref() {
Expr::Tuple(ast::ExprTuple { elts, .. }) => {
// Traverse each element of the tuple within the union recursively to handle cases
// such as `Union[..., Union[...]]
elts.iter()
.for_each(|elt| inner(func, semantic, elt, Some(expr)));
return;
}
other => {
inner(func, semantic, other, Some(expr));
}
}
}
}
Expand Down