2Tests for annotating generators.
5# Specification: https://typing.readthedocs.io/en/latest/spec/annotations.html#annotating-generator-functions-and-coroutines
7# The return type of generator functions can be annotated by the generic type
8# Generator[yield_type, send_type, return_type] provided by typing.py module.
35 def should_continue(self) -> bool:
43def generator1() -> Generator[A, B, C]:
45 while cont.should_continue():
51def generator2() -> Generator[A, B, C]: # E: missing return
Expected a ty diagnostic for this line
53 if cont.should_continue():
54 return False # E: incompatible return type
Expected a ty diagnostic for this line
56 while cont.should_continue():
57 yield 3 # E: incompatible yield type
Expected a ty diagnostic for this line
60def generator3() -> Generator[A, int, Any]:
62 if cont.should_continue():
65 while cont.should_continue():
66 yield 3 # E: Incompatible yield type
Expected a ty diagnostic for this line
69def generator4() -> Iterable[A]:
71 return True # E?: No return value expected
74def generator5() -> Iterator[A]:
75 yield B() # E: incompatible yield type
Expected a ty diagnostic for this line
78def generator6() -> Generator[None, None, None]:
82def generator7() -> Iterator[dict[str, int]]:
86def generator8() -> int: # E: incompatible return type
[invalid-return-type] Return type does not match returned value: expected `int`, found `types.GeneratorType`
91async def generator9() -> int: # E: incompatible return type
[invalid-return-type] Return type does not match returned value: expected `int`, found `types.AsyncGeneratorType`
95class IntIterator(Protocol):
96 def __next__(self, /) -> int:
100def generator15() -> IntIterator: # OK
104class AsyncIntIterator(Protocol):
105 def __anext__(self, /) -> Awaitable[int]:
109async def generator16() -> AsyncIntIterator: # OK
113def generator17() -> Iterator[A]: # OK
114 yield from generator17()
117def generator18() -> Iterator[B]:
118 yield from generator17() # E: incompatible generator type
Expected a ty diagnostic for this line
119 yield from [1] # E: incompatible generator type
Expected a ty diagnostic for this line
122def generator19() -> Generator[None, float, None]: # OK
126def generator20() -> Generator[None, int, None]: # OK
127 yield from generator19()
130def generator21() -> Generator[None, int, None]:
134def generator22() -> Generator[None, str, None]:
135 yield from generator21() # E: incompatible send type
Expected a ty diagnostic for this line
138def generator23() -> Iterable[str]: # OK
140 yield "" # Unreachable
143async def generator24() -> AsyncIterable[str]: # OK
145 yield "" # Unreachable
148def generator25(ints1: list[int], ints2: list[int]) -> Generator[int, None, None]: # OK
153async def get_data() -> list[int]:
154 await asyncio.sleep(1)
158async def generator26(nums: list[int]) -> AsyncGenerator[str, None]:
160 await asyncio.sleep(1)
161 yield f"The number is {n}"
164async def generator27() -> AsyncGenerator[str, None]:
165 data = await get_data()
166 v1 = generator26(data)
167 assert_type(v1, AsyncGenerator[str, None])
171async def generator28() -> AsyncIterator[str]:
172 data = await get_data()
173 v1 = generator26(data)
174 assert_type(v1, AsyncGenerator[str, None])
178async def generator29() -> AsyncIterator[int]:
179 raise NotImplementedError
182# Don't use assert_type here because some type checkers infer
183# the narrower type types.CoroutineType rather than typing.Coroutine
185v1: Callable[[], Coroutine[Any, Any, AsyncIterator[int]]] = generator29
188async def generator30() -> AsyncIterator[int]:
189 raise NotImplementedError
193assert_type(generator30, Callable[[], AsyncIterator[int]])