Skip to content

Commit bbb044e

Browse files
authored
Detect tuples bound to variadic positional arguments i.e. *args (#13512)
In #13503, we added supported for detecting variadic keyword arguments as dictionaries, here we use the same strategy for detecting variadic positional arguments as tuples.
1 parent 4810652 commit bbb044e

File tree

1 file changed

+20
-4
lines changed
  • crates/ruff_python_semantic/src/analyze

1 file changed

+20
-4
lines changed

crates/ruff_python_semantic/src/analyze/typing.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl TypeChecker for IoBaseChecker {
731731
/// Test whether the given binding can be considered a list.
732732
///
733733
/// For this, we check what value might be associated with it through it's initialization and
734-
/// what annotation it has (we consider `list` and `typing.List`).
734+
/// what annotation it has (we consider `list` and `typing.List`)
735735
pub fn is_list(binding: &Binding, semantic: &SemanticModel) -> bool {
736736
check_type::<ListChecker>(binding, semantic)
737737
}
@@ -771,10 +771,26 @@ pub fn is_set(binding: &Binding, semantic: &SemanticModel) -> bool {
771771

772772
/// Test whether the given binding can be considered a tuple.
773773
///
774-
/// For this, we check what value might be associated with it through
775-
/// it's initialization and what annotation it has (we consider `tuple` and
776-
/// `typing.Tuple`).
774+
/// For this, we check what value might be associated with it through it's initialization, what
775+
/// annotation it has (we consider `tuple` and `typing.Tuple`), and if it is a variadic positional
776+
/// argument.
777777
pub fn is_tuple(binding: &Binding, semantic: &SemanticModel) -> bool {
778+
// ```python
779+
// def foo(*args):
780+
// ...
781+
// ```
782+
if matches!(binding.kind, BindingKind::Argument) {
783+
if let Some(Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })) =
784+
binding.statement(semantic)
785+
{
786+
if let Some(arg_parameter) = parameters.vararg.as_deref() {
787+
if arg_parameter.name.range() == binding.range() {
788+
return true;
789+
}
790+
}
791+
}
792+
}
793+
778794
check_type::<TupleChecker>(binding, semantic)
779795
}
780796

0 commit comments

Comments
 (0)