mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 16:10:17 +00:00
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:
parent
481065238b
commit
bbb044ebda
1 changed files with 20 additions and 4 deletions
|
@ -731,7 +731,7 @@ impl TypeChecker for IoBaseChecker {
|
||||||
/// Test whether the given binding can be considered a list.
|
/// 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
|
/// 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 {
|
pub fn is_list(binding: &Binding, semantic: &SemanticModel) -> bool {
|
||||||
check_type::<ListChecker>(binding, semantic)
|
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.
|
/// Test whether the given binding can be considered a tuple.
|
||||||
///
|
///
|
||||||
/// For this, we check what value might be associated with it through
|
/// For this, we check what value might be associated with it through it's initialization, what
|
||||||
/// it's initialization and what annotation it has (we consider `tuple` and
|
/// annotation it has (we consider `tuple` and `typing.Tuple`), and if it is a variadic positional
|
||||||
/// `typing.Tuple`).
|
/// argument.
|
||||||
pub fn is_tuple(binding: &Binding, semantic: &SemanticModel) -> bool {
|
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)
|
check_type::<TupleChecker>(binding, semantic)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue