mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:47 +00:00
[red-knot] Improve type inference for iteration over heterogenous tuples (#13314)
Followup to #13295
This commit is contained in:
parent
a528edad35
commit
2ca78721e6
2 changed files with 34 additions and 0 deletions
|
@ -408,6 +408,18 @@ impl<'db> Type<'db> {
|
||||||
/// pass
|
/// pass
|
||||||
/// ```
|
/// ```
|
||||||
fn iterate(&self, db: &'db dyn Db) -> IterationOutcome<'db> {
|
fn iterate(&self, db: &'db dyn Db) -> IterationOutcome<'db> {
|
||||||
|
if let Type::Tuple(tuple_type) = self {
|
||||||
|
return IterationOutcome::Iterable {
|
||||||
|
element_ty: tuple_type
|
||||||
|
.elements(db)
|
||||||
|
.iter()
|
||||||
|
.fold(UnionBuilder::new(db), |builder, element| {
|
||||||
|
builder.add(*element)
|
||||||
|
})
|
||||||
|
.build(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// `self` represents the type of the iterable;
|
// `self` represents the type of the iterable;
|
||||||
// `__iter__` and `__next__` are both looked up on the class of the iterable:
|
// `__iter__` and `__next__` are both looked up on the class of the iterable:
|
||||||
let iterable_meta_type = self.to_meta_type(db);
|
let iterable_meta_type = self.to_meta_type(db);
|
||||||
|
|
|
@ -4355,6 +4355,28 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn for_loop_with_heterogenous_tuple() -> anyhow::Result<()> {
|
||||||
|
let mut db = setup_db();
|
||||||
|
|
||||||
|
db.write_dedented(
|
||||||
|
"src/a.py",
|
||||||
|
"
|
||||||
|
for x in (1, 'a', b'foo'):
|
||||||
|
pass
|
||||||
|
",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
assert_public_ty(
|
||||||
|
&db,
|
||||||
|
"src/a.py",
|
||||||
|
"x",
|
||||||
|
r#"Literal[1] | Literal["a"] | Literal[b"foo"]"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn except_handler_single_exception() -> anyhow::Result<()> {
|
fn except_handler_single_exception() -> anyhow::Result<()> {
|
||||||
let mut db = setup_db();
|
let mut db = setup_db();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue