Detect tuples bound to variadic positional arguments i.e. *args (#13512)

In https://github.com/astral-sh/ruff/pull/13503, we added supported for
detecting variadic keyword arguments as dictionaries, here we use the
same strategy for detecting variadic positional arguments as tuples.
This commit is contained in:
Zanie Blue 2024-09-25 10:03:25 -05:00 committed by GitHub
parent 481065238b
commit bbb044ebda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -731,7 +731,7 @@ impl TypeChecker for IoBaseChecker {
/// Test whether the given binding can be considered a list.
///
/// For this, we check what value might be associated with it through it's initialization and
/// what annotation it has (we consider `list` and `typing.List`).
/// what annotation it has (we consider `list` and `typing.List`)
pub fn is_list(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<ListChecker>(binding, semantic)
}
@ -771,10 +771,26 @@ pub fn is_set(binding: &Binding, semantic: &SemanticModel) -> bool {
/// Test whether the given binding can be considered a tuple.
///
/// For this, we check what value might be associated with it through
/// it's initialization and what annotation it has (we consider `tuple` and
/// `typing.Tuple`).
/// For this, we check what value might be associated with it through it's initialization, what
/// annotation it has (we consider `tuple` and `typing.Tuple`), and if it is a variadic positional
/// argument.
pub fn is_tuple(binding: &Binding, semantic: &SemanticModel) -> bool {
// ```python
// def foo(*args):
// ...
// ```
if matches!(binding.kind, BindingKind::Argument) {
if let Some(Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })) =
binding.statement(semantic)
{
if let Some(arg_parameter) = parameters.vararg.as_deref() {
if arg_parameter.name.range() == binding.range() {
return true;
}
}
}
}
check_type::<TupleChecker>(binding, semantic)
}