[flake8-pyi, ruff] Fix traversal of nested literals and unions (PYI016, PYI051, PYI055, PYI062, RUF041) (#14641)

This commit is contained in:
Simon Brugman 2024-11-28 19:07:12 +01:00 committed by GitHub
parent d9cbf2fe44
commit dc29f52750
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 318 additions and 169 deletions

View file

@ -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));
}
}