Skip to content

Commit ae99cad

Browse files
brandtbucherilevkivskyi
authored andcommitted
Fix yield from TupleType. (#6902)
Fixes #4444 by removing special-casing for instance types. Now `yield from <tuple>` works as expected.
1 parent 1179a27 commit ae99cad

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

mypy/checker.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,15 +2442,14 @@ def split_around_star(self, items: List[T], star_index: int,
24422442
def type_is_iterable(self, type: Type) -> bool:
24432443
if isinstance(type, CallableType) and type.is_type_obj():
24442444
type = type.fallback
2445-
return (is_subtype(type, self.named_generic_type('typing.Iterable',
2446-
[AnyType(TypeOfAny.special_form)])) and
2447-
isinstance(type, Instance))
2445+
return is_subtype(type, self.named_generic_type('typing.Iterable',
2446+
[AnyType(TypeOfAny.special_form)]))
24482447

24492448
def check_multi_assignment_from_iterable(self, lvalues: List[Lvalue], rvalue_type: Type,
24502449
context: Context,
24512450
infer_lvalue_type: bool = True) -> None:
2452-
if self.type_is_iterable(rvalue_type):
2453-
item_type = self.iterable_item_type(cast(Instance, rvalue_type))
2451+
if self.type_is_iterable(rvalue_type) and isinstance(rvalue_type, Instance):
2452+
item_type = self.iterable_item_type(rvalue_type)
24542453
for lv in lvalues:
24552454
if isinstance(lv, StarExpr):
24562455
items_type = self.named_generic_type('builtins.list', [item_type])

test-data/unit/check-expressions.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,16 @@ def g() -> Iterator[int]:
16941694
a = yield from f()
16951695
[out]
16961696

1697+
[case testYieldFromTupleExpression]
1698+
from typing import Generator
1699+
def g() -> Generator[int, None, None]:
1700+
x = yield from () # E: Function does not return a value
1701+
x = yield from (0, 1, 2) # E: Function does not return a value
1702+
x = yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "object", expected type "int") \
1703+
# E: Function does not return a value
1704+
x = yield from ("ERROR",) # E: Incompatible types in "yield from" (actual type "str", expected type "int") \
1705+
# E: Function does not return a value
1706+
[builtins fixtures/tuple.pyi]
16971707

16981708
-- dict(...)
16991709
-- ---------

test-data/unit/check-statements.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,15 @@ def g() -> Generator[int, str, float]:
12501250
reveal_type(r) # E: Revealed type is 'builtins.str*'
12511251
return 3.14
12521252

1253+
[case testYieldFromTupleStatement]
1254+
from typing import Generator
1255+
def g() -> Generator[int, None, None]:
1256+
yield from ()
1257+
yield from (0, 1, 2)
1258+
yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "object", expected type "int")
1259+
yield from ("ERROR",) # E: Incompatible types in "yield from" (actual type "str", expected type "int")
1260+
[builtins fixtures/tuple.pyi]
1261+
12531262
-- With statement
12541263
-- --------------
12551264

0 commit comments

Comments
 (0)