mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Don't skip visiting non-tuple slice in typing.Annotated
subscripts (#17201)
Fixes: #17196 ## Summary Skipping these nodes for malformed type expressions would lead to incorrect semantic state, which can in turn mean we emit false positives for rules like `unused-variable`(`F841`) ## Test Plan `cargo nextest run`
This commit is contained in:
parent
5cee346744
commit
33a56f198b
2 changed files with 27 additions and 1 deletions
|
@ -1669,8 +1669,16 @@ impl<'a> Visitor<'a> for Checker<'a> {
|
||||||
}
|
}
|
||||||
self.visit_expr_context(ctx);
|
self.visit_expr_context(ctx);
|
||||||
} else {
|
} else {
|
||||||
|
if self.semantic.in_type_definition() {
|
||||||
|
// this should potentially trigger some kind of violation in the
|
||||||
|
// future, since it would indicate an invalid type expression
|
||||||
debug!("Found non-Expr::Tuple argument to PEP 593 Annotation.");
|
debug!("Found non-Expr::Tuple argument to PEP 593 Annotation.");
|
||||||
}
|
}
|
||||||
|
// even if the expression is invalid as a type expression, we should
|
||||||
|
// still visit it so we don't accidentally treat variables as unused
|
||||||
|
self.visit_expr(slice);
|
||||||
|
self.visit_expr_context(ctx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(typing::SubscriptKind::TypedDict) => {
|
Some(typing::SubscriptKind::TypedDict) => {
|
||||||
if let Expr::Dict(ast::ExprDict { items, range: _ }) = slice.as_ref() {
|
if let Expr::Dict(ast::ExprDict { items, range: _ }) = slice.as_ref() {
|
||||||
|
|
|
@ -4263,4 +4263,22 @@ lambda: fu
|
||||||
&[],
|
&[],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gh_issue_17196_regression_test() {
|
||||||
|
flakes(
|
||||||
|
r#"
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
def type_annotations_from_tuple():
|
||||||
|
annos = (str, "foo", "bar")
|
||||||
|
return Annotated[annos]
|
||||||
|
|
||||||
|
def type_annotations_from_filtered_tuple():
|
||||||
|
annos = (str, None, "foo", None, "bar")
|
||||||
|
return Annotated[tuple([a for a in annos if a is not None])]
|
||||||
|
"#,
|
||||||
|
&[],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue