mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
[flake8-pyi
, ruff
] Fix traversal of nested literals and unions (PYI016
, PYI051
, PYI055
, PYI062
, RUF041
) (#14641)
This commit is contained in:
parent
d9cbf2fe44
commit
dc29f52750
16 changed files with 318 additions and 169 deletions
|
@ -373,7 +373,7 @@ where
|
|||
) where
|
||||
F: FnMut(&'a Expr, &'a Expr),
|
||||
{
|
||||
// Ex) x | y
|
||||
// Ex) `x | y`
|
||||
if let Expr::BinOp(ast::ExprBinOp {
|
||||
op: Operator::BitOr,
|
||||
left,
|
||||
|
@ -396,17 +396,21 @@ where
|
|||
return;
|
||||
}
|
||||
|
||||
// Ex) Union[x, y]
|
||||
// Ex) `Union[x, y]`
|
||||
if let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = expr {
|
||||
if semantic.match_typing_expr(value, "Union") {
|
||||
if let Expr::Tuple(tuple) = &**slice {
|
||||
// Traverse each element of the tuple within the union recursively to handle cases
|
||||
// such as `Union[..., Union[...]]
|
||||
// such as `Union[..., Union[...]]`
|
||||
tuple
|
||||
.iter()
|
||||
.for_each(|elem| inner(func, semantic, elem, Some(expr)));
|
||||
return;
|
||||
}
|
||||
|
||||
// Ex) `Union[Union[a, b]]` and `Union[a | b | c]`
|
||||
inner(func, semantic, slice, Some(expr));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -435,18 +439,19 @@ where
|
|||
) where
|
||||
F: FnMut(&'a Expr, &'a Expr),
|
||||
{
|
||||
// Ex) Literal[x, y]
|
||||
// Ex) `Literal[x, y]`
|
||||
if let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = expr {
|
||||
if semantic.match_typing_expr(value, "Literal") {
|
||||
match &**slice {
|
||||
Expr::Tuple(tuple) => {
|
||||
// Traverse each element of the tuple within the literal recursively to handle cases
|
||||
// such as `Literal[..., Literal[...]]
|
||||
// such as `Literal[..., Literal[...]]`
|
||||
for element in tuple {
|
||||
inner(func, semantic, element, Some(expr));
|
||||
}
|
||||
}
|
||||
other => {
|
||||
// Ex) `Literal[Literal[...]]`
|
||||
inner(func, semantic, other, Some(expr));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue