mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Use match_typing_call_path
in more sites (#9336)
Preferable as it avoids multiple `resolve_call_path` calls internally.
This commit is contained in:
parent
195f7c097a
commit
686abbc97a
3 changed files with 38 additions and 23 deletions
|
@ -191,7 +191,11 @@ fn match_annotation_to_complex_bool(annotation: &Expr, semantic: &SemanticModel)
|
||||||
}
|
}
|
||||||
// Ex) `typing.Union[bool, int]`
|
// Ex) `typing.Union[bool, int]`
|
||||||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||||
if semantic.match_typing_expr(value, "Union") {
|
let call_path = semantic.resolve_call_path(value);
|
||||||
|
if call_path
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|call_path| semantic.match_typing_call_path(call_path, "Union"))
|
||||||
|
{
|
||||||
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
|
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
|
||||||
elts.iter()
|
elts.iter()
|
||||||
.any(|elt| match_annotation_to_complex_bool(elt, semantic))
|
.any(|elt| match_annotation_to_complex_bool(elt, semantic))
|
||||||
|
@ -199,7 +203,10 @@ fn match_annotation_to_complex_bool(annotation: &Expr, semantic: &SemanticModel)
|
||||||
// Union with a single type is an invalid type annotation
|
// Union with a single type is an invalid type annotation
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
} else if semantic.match_typing_expr(value, "Optional") {
|
} else if call_path
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|call_path| semantic.match_typing_call_path(call_path, "Optional"))
|
||||||
|
{
|
||||||
match_annotation_to_complex_bool(slice, semantic)
|
match_annotation_to_complex_bool(slice, semantic)
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
|
|
@ -244,7 +244,12 @@ fn non_none_annotation_element<'a>(
|
||||||
) -> Option<&'a Expr> {
|
) -> Option<&'a Expr> {
|
||||||
// E.g., `typing.Union` or `typing.Optional`
|
// E.g., `typing.Union` or `typing.Optional`
|
||||||
if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation {
|
if let Expr::Subscript(ExprSubscript { value, slice, .. }) = annotation {
|
||||||
if semantic.match_typing_expr(value, "Optional") {
|
let call_path = semantic.resolve_call_path(value);
|
||||||
|
|
||||||
|
if call_path
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|value| semantic.match_typing_call_path(value, "Optional"))
|
||||||
|
{
|
||||||
return if slice.is_none_literal_expr() {
|
return if slice.is_none_literal_expr() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -252,7 +257,10 @@ fn non_none_annotation_element<'a>(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if !semantic.match_typing_expr(value, "Union") {
|
if !call_path
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|value| semantic.match_typing_call_path(value, "Union"))
|
||||||
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,30 +76,30 @@ impl<'a> TypingTarget<'a> {
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||||
if semantic.match_typing_expr(value, "Optional") {
|
semantic.resolve_call_path(value).map_or(
|
||||||
Some(TypingTarget::Optional(slice.as_ref()))
|
// If we can't resolve the call path, it must be defined
|
||||||
} else if semantic.match_typing_expr(value, "Literal") {
|
// in the same file and could be a type alias.
|
||||||
Some(TypingTarget::Literal(slice))
|
Some(TypingTarget::Unknown),
|
||||||
} else if semantic.match_typing_expr(value, "Union") {
|
|call_path| {
|
||||||
Some(TypingTarget::Union(slice))
|
if semantic.match_typing_call_path(&call_path, "Optional") {
|
||||||
} else if semantic.match_typing_expr(value, "Annotated") {
|
Some(TypingTarget::Optional(slice.as_ref()))
|
||||||
resolve_slice_value(slice.as_ref())
|
} else if semantic.match_typing_call_path(&call_path, "Literal") {
|
||||||
.next()
|
Some(TypingTarget::Literal(slice.as_ref()))
|
||||||
.map(TypingTarget::Annotated)
|
} else if semantic.match_typing_call_path(&call_path, "Union") {
|
||||||
} else {
|
Some(TypingTarget::Union(slice.as_ref()))
|
||||||
semantic.resolve_call_path(value).map_or(
|
} else if semantic.match_typing_call_path(&call_path, "Annotated") {
|
||||||
// If we can't resolve the call path, it must be defined
|
resolve_slice_value(slice.as_ref())
|
||||||
// in the same file and could be a type alias.
|
.next()
|
||||||
Some(TypingTarget::Unknown),
|
.map(TypingTarget::Annotated)
|
||||||
|call_path| {
|
} else {
|
||||||
if is_known_type(&call_path, minor_version) {
|
if is_known_type(&call_path, minor_version) {
|
||||||
Some(TypingTarget::Known)
|
Some(TypingTarget::Known)
|
||||||
} else {
|
} else {
|
||||||
Some(TypingTarget::Unknown)
|
Some(TypingTarget::Unknown)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
)
|
},
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
Expr::BinOp(ast::ExprBinOp {
|
Expr::BinOp(ast::ExprBinOp {
|
||||||
left,
|
left,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue