Skip to content

Commit 4a76a1a

Browse files
authored
Add missing lineno to yield from with wrong type (#18518)
Fixes #18517
1 parent 905ea7b commit 4a76a1a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,6 +6141,7 @@ def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = Fals
61416141
generic_generator_type = self.chk.named_generic_type(
61426142
"typing.Generator", [any_type, any_type, any_type]
61436143
)
6144+
generic_generator_type.set_line(e)
61446145
iter_type, _ = self.check_method_call_by_name(
61456146
"__iter__", subexpr_type, [], [], context=generic_generator_type
61466147
)

test-data/unit/check-statements.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,20 @@ def get_strings(foo: bool) -> Iterator[str]:
22832283
yield "bar2"
22842284
[builtins fixtures/tuple.pyi]
22852285

2286+
[case testYieldFromInvalidType]
2287+
from collections.abc import Iterator
2288+
2289+
class A:
2290+
def list(self) -> None: ...
2291+
2292+
def foo(self) -> list[int]: # E: Function "__main__.A.list" is not valid as a type \
2293+
# N: Perhaps you need "Callable[...]" or a callback protocol?
2294+
return []
2295+
2296+
def fn() -> Iterator[int]:
2297+
yield from A().foo() # E: "list?[builtins.int]" has no attribute "__iter__" (not iterable)
2298+
[builtins fixtures/tuple.pyi]
2299+
22862300
[case testNoCrashOnStarRightHandSide]
22872301
x = *(1, 2, 3) # E: can't use starred expression here
22882302
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)