Infer list[T] for starred target in unpacking (#18401)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / mkdocs (push) Waiting to run
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Closes: astral-sh/ty#191

## Test Plan

Update existing tests.
This commit is contained in:
Dhruv Manilawala 2025-06-03 07:25:07 +05:30 committed by GitHub
parent 14c42a8ddf
commit 2289187b74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 35 deletions

View file

@ -8,12 +8,12 @@ use ruff_python_ast::{self as ast, AnyNodeRef};
use crate::Db;
use crate::semantic_index::ast_ids::{HasScopedExpressionId, ScopedExpressionId};
use crate::semantic_index::symbol::ScopeId;
use crate::types::{Type, TypeCheckDiagnostics, infer_expression_types, todo_type};
use crate::types::{Type, TypeCheckDiagnostics, infer_expression_types};
use crate::unpack::{UnpackKind, UnpackValue};
use super::context::InferContext;
use super::diagnostic::INVALID_ASSIGNMENT;
use super::{TupleType, UnionType};
use super::{KnownClass, TupleType, UnionType};
/// Unpacks the value expression type to their respective targets.
pub(crate) struct Unpacker<'db> {
@ -253,12 +253,17 @@ impl<'db> Unpacker<'db> {
let starred_end_index = tuple_ty.len(self.db()) - remaining;
// SAFETY: Safe because of the length check above.
let _starred_element_types =
let starred_element_types =
&tuple_ty.elements(self.db())[starred_index..starred_end_index];
// TODO: Combine the types into a list type. If the
// starred_element_types is empty, then it should be `List[Any]`.
// combine_types(starred_element_types);
element_types.push(todo_type!("starred unpacking"));
element_types.push(KnownClass::List.to_specialized_instance(
self.db(),
[if starred_element_types.is_empty() {
Type::unknown()
} else {
UnionType::from_elements(self.db(), starred_element_types)
}],
));
// Insert the types remaining that aren't consumed by the starred expression.
element_types.extend_from_slice(
@ -278,7 +283,18 @@ impl<'db> Unpacker<'db> {
);
}
Cow::Owned(vec![Type::unknown(); targets.len()])
Cow::Owned(
targets
.iter()
.map(|target| {
if target.is_starred_expr() {
KnownClass::List.to_specialized_instance(self.db(), [Type::unknown()])
} else {
Type::unknown()
}
})
.collect(),
)
}
}