Skip to content

Commit 6f468ae

Browse files
committed
improve
1 parent a377c0a commit 6f468ae

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

crates/ty_python_semantic/resources/mdtest/loops/for.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,20 @@ def g(
264264
reveal_type(y) # revealed: str | int
265265
```
266266

267-
## Union type as iterable where all elements in the union have precise tuple specs
267+
## Union type as iterable where some elements in the union have precise tuple specs
268268

269-
If all elements in a union can be heterogeneously unpacked, we "union together" their "tuple specs"
270-
and are able to infer the iterable element precisely when iterating over the union, in the same way
271-
that we infer a precise type for the iterable element when iterating over a `Literal` string or
272-
bytes type:
269+
If all elements in a union can be iterated over, we "union together" their "tuple specs" and are
270+
able to infer the iterable element precisely when iterating over the union, in the same way that we
271+
infer a precise type for the iterable element when iterating over a `Literal` string or bytes type:
273272

274273
```py
275274
from typing import Literal
276275

277-
def f(x: Literal["foo", b"bar"]):
276+
def f(x: Literal["foo", b"bar"], y: Literal["foo"] | range):
278277
for item in x:
279278
reveal_type(item) # revealed: Literal["f", "o", 98, 97, 114]
279+
for item in y:
280+
reveal_type(item) # revealed: Literal["f", "o"] | int
280281
```
281282

282283
## Union type as iterable where one union element has no `__iter__` method

crates/ty_python_semantic/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5358,10 +5358,10 @@ impl<'db> Type<'db> {
53585358
let elements = union.elements(db);
53595359
if elements.len() < MAX_TUPLE_LENGTH {
53605360
let mut elements_iter = elements.iter();
5361-
let first_element_spec = non_async_special_case(db, *elements_iter.next()?)?;
5361+
let first_element_spec = elements_iter.next()?.try_iterate_with_mode(db, EvaluationMode::Sync).ok()?;
53625362
let mut builder = TupleSpecBuilder::from(&*first_element_spec);
53635363
for element in elements_iter {
5364-
builder = builder.union(db, non_async_special_case(db, *element).as_deref()?);
5364+
builder = builder.union(db, &*element.try_iterate_with_mode(db, EvaluationMode::Sync).ok()?);
53655365
}
53665366
Some(Cow::Owned(builder.build()))
53675367
} else {

0 commit comments

Comments
 (0)