| 1 |
-- Tests for async def and await (PEP 492) |
| 2 |
-- --------------------------------------- |
| 3 |
|
| 4 |
[case testAsyncDefPass] |
| 5 |
|
| 6 |
async def f() -> int: |
| 7 |
pass |
| 8 |
[builtins fixtures/async_await.pyi] |
| 9 |
[typing fixtures/typing-async.pyi] |
| 10 |
|
| 11 |
[case testAsyncDefReturn] |
| 12 |
|
| 13 |
async def f() -> int: |
| 14 |
return 0 |
| 15 |
_ = reveal_type(f()) # N: Revealed type is "typing.Coroutine[Any, Any, builtins.int]" |
| 16 |
[builtins fixtures/async_await.pyi] |
| 17 |
[typing fixtures/typing-async.pyi] |
| 18 |
|
| 19 |
[case testAsyncDefMissingReturn] |
| 20 |
# flags: --warn-no-return |
| 21 |
async def f() -> int: |
| 22 |
make_this_not_trivial = 1 |
| 23 |
[builtins fixtures/async_await.pyi] |
| 24 |
[typing fixtures/typing-async.pyi] |
| 25 |
[out] |
| 26 |
main:2: error: Missing return statement |
| 27 |
|
| 28 |
[case testAsyncDefReturnWithoutValue] |
| 29 |
|
| 30 |
async def f() -> int: |
| 31 |
make_this_not_trivial = 1 |
| 32 |
return |
| 33 |
[builtins fixtures/async_await.pyi] |
| 34 |
[typing fixtures/typing-async.pyi] |
| 35 |
[out] |
| 36 |
main:4: error: Return value expected |
| 37 |
|
| 38 |
[case testAwaitCoroutine] |
| 39 |
|
| 40 |
async def f() -> int: |
| 41 |
x = await f() |
| 42 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 43 |
return x |
| 44 |
[builtins fixtures/async_await.pyi] |
| 45 |
[typing fixtures/typing-async.pyi] |
| 46 |
[out] |
| 47 |
|
| 48 |
[case testAwaitDefaultContext] |
| 49 |
|
| 50 |
from typing import TypeVar |
| 51 |
T = TypeVar('T') |
| 52 |
async def f(x: T) -> T: |
| 53 |
y = await f(x) |
| 54 |
reveal_type(y) |
| 55 |
return y |
| 56 |
[typing fixtures/typing-async.pyi] |
| 57 |
[out] |
| 58 |
main:6: note: Revealed type is "T`-1" |
| 59 |
|
| 60 |
[case testAwaitAnyContext] |
| 61 |
|
| 62 |
from typing import Any, TypeVar |
| 63 |
T = TypeVar('T') |
| 64 |
async def f(x: T) -> T: |
| 65 |
y = await f(x) # type: Any |
| 66 |
reveal_type(y) |
| 67 |
return y |
| 68 |
[typing fixtures/typing-async.pyi] |
| 69 |
[out] |
| 70 |
main:6: note: Revealed type is "Any" |
| 71 |
|
| 72 |
[case testAwaitExplicitContext] |
| 73 |
|
| 74 |
from typing import TypeVar |
| 75 |
T = TypeVar('T') |
| 76 |
async def f(x: T) -> T: |
| 77 |
y = await f(x) # type: int |
| 78 |
reveal_type(y) |
| 79 |
return x |
| 80 |
[typing fixtures/typing-async.pyi] |
| 81 |
[out] |
| 82 |
main:5: error: Argument 1 to "f" has incompatible type "T"; expected "int" |
| 83 |
main:6: note: Revealed type is "builtins.int" |
| 84 |
|
| 85 |
[case testAwaitGeneratorError] |
| 86 |
|
| 87 |
from typing import Any, Generator |
| 88 |
def g() -> Generator[int, None, str]: |
| 89 |
yield 0 |
| 90 |
return '' |
| 91 |
async def f() -> int: |
| 92 |
x = await g() |
| 93 |
return x |
| 94 |
[typing fixtures/typing-async.pyi] |
| 95 |
[out] |
| 96 |
main:7: error: Incompatible types in "await" (actual type "Generator[int, None, str]", expected type "Awaitable[Any]") |
| 97 |
|
| 98 |
[case testAwaitIteratorError] |
| 99 |
|
| 100 |
from typing import Any, Iterator |
| 101 |
def g() -> Iterator[Any]: |
| 102 |
yield |
| 103 |
async def f() -> int: |
| 104 |
x = await g() |
| 105 |
return x |
| 106 |
[typing fixtures/typing-async.pyi] |
| 107 |
[out] |
| 108 |
main:6: error: Incompatible types in "await" (actual type "Iterator[Any]", expected type "Awaitable[Any]") |
| 109 |
|
| 110 |
[case testAwaitArgumentError] |
| 111 |
|
| 112 |
def g() -> int: |
| 113 |
return 0 |
| 114 |
async def f() -> int: |
| 115 |
x = await g() |
| 116 |
return x |
| 117 |
[builtins fixtures/async_await.pyi] |
| 118 |
[typing fixtures/typing-async.pyi] |
| 119 |
[out] |
| 120 |
main:5: error: Incompatible types in "await" (actual type "int", expected type "Awaitable[Any]") |
| 121 |
|
| 122 |
[case testAwaitResultError] |
| 123 |
|
| 124 |
async def g() -> int: |
| 125 |
return 0 |
| 126 |
async def f() -> str: |
| 127 |
x = await g() # type: str |
| 128 |
return x |
| 129 |
[builtins fixtures/async_await.pyi] |
| 130 |
[typing fixtures/typing-async.pyi] |
| 131 |
[out] |
| 132 |
main:5: error: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 133 |
|
| 134 |
[case testAwaitReturnError] |
| 135 |
|
| 136 |
async def g() -> int: |
| 137 |
return 0 |
| 138 |
async def f() -> str: |
| 139 |
x = await g() |
| 140 |
return x |
| 141 |
[builtins fixtures/async_await.pyi] |
| 142 |
[typing fixtures/typing-async.pyi] |
| 143 |
[out] |
| 144 |
main:6: error: Incompatible return value type (got "int", expected "str") |
| 145 |
|
| 146 |
[case testAsyncFor] |
| 147 |
|
| 148 |
from typing import AsyncIterator |
| 149 |
class C(AsyncIterator[int]): |
| 150 |
async def __anext__(self) -> int: return 0 |
| 151 |
async def f() -> None: |
| 152 |
async for x in C(): |
| 153 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 154 |
[builtins fixtures/async_await.pyi] |
| 155 |
[typing fixtures/typing-async.pyi] |
| 156 |
|
| 157 |
[case testAsyncForError] |
| 158 |
|
| 159 |
from typing import AsyncIterator |
| 160 |
async def f() -> None: |
| 161 |
async for x in [1]: |
| 162 |
pass |
| 163 |
[builtins fixtures/async_await.pyi] |
| 164 |
[typing fixtures/typing-async.pyi] |
| 165 |
[out] |
| 166 |
main:4: error: "list[int]" has no attribute "__aiter__" (not async iterable) |
| 167 |
|
| 168 |
[case testAsyncForErrorNote] |
| 169 |
|
| 170 |
from typing import AsyncIterator, AsyncGenerator |
| 171 |
async def g() -> AsyncGenerator[str, None]: |
| 172 |
pass |
| 173 |
|
| 174 |
async def f() -> None: |
| 175 |
async for x in g(): |
| 176 |
pass |
| 177 |
[builtins fixtures/async_await.pyi] |
| 178 |
[typing fixtures/typing-async.pyi] |
| 179 |
[out] |
| 180 |
main:7: error: "Coroutine[Any, Any, AsyncGenerator[str, None]]" has no attribute "__aiter__" (not async iterable) |
| 181 |
main:7: note: Maybe you forgot to use "await"? |
| 182 |
|
| 183 |
[case testAsyncForErrorCanBeIgnored] |
| 184 |
|
| 185 |
from typing import AsyncIterator, AsyncGenerator |
| 186 |
async def g() -> AsyncGenerator[str, None]: |
| 187 |
pass |
| 188 |
|
| 189 |
async def f() -> None: |
| 190 |
async for x in g(): # type: ignore[attr-defined] |
| 191 |
pass |
| 192 |
[builtins fixtures/async_await.pyi] |
| 193 |
[typing fixtures/typing-async.pyi] |
| 194 |
|
| 195 |
[case testAsyncForTypeComments_no_native_parse] |
| 196 |
|
| 197 |
from typing import AsyncIterator, Union |
| 198 |
class C(AsyncIterator[int]): |
| 199 |
async def __anext__(self) -> int: return 0 |
| 200 |
async def f() -> None: |
| 201 |
async for x in C(): # type: str # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 202 |
pass |
| 203 |
|
| 204 |
async for y in C(): # type: int |
| 205 |
pass |
| 206 |
|
| 207 |
async for z in C(): # type: Union[int, str] |
| 208 |
reveal_type(z) # N: Revealed type is "builtins.int | builtins.str" |
| 209 |
[builtins fixtures/async_await.pyi] |
| 210 |
[typing fixtures/typing-async.pyi] |
| 211 |
|
| 212 |
[case testAsyncForComprehension] |
| 213 |
from typing import Generic, Iterable, TypeVar, AsyncIterator, Tuple |
| 214 |
|
| 215 |
T = TypeVar('T') |
| 216 |
|
| 217 |
class asyncify(Generic[T], AsyncIterator[T]): |
| 218 |
def __init__(self, iterable: Iterable[T]) -> None: |
| 219 |
self.iterable = iter(iterable) |
| 220 |
def __aiter__(self) -> AsyncIterator[T]: |
| 221 |
return self |
| 222 |
async def __anext__(self) -> T: |
| 223 |
try: |
| 224 |
return next(self.iterable) |
| 225 |
except StopIteration: |
| 226 |
raise StopAsyncIteration |
| 227 |
|
| 228 |
async def listcomp(obj: Iterable[int]): |
| 229 |
lst = [i async for i in asyncify(obj)] |
| 230 |
reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int]" |
| 231 |
lst2 = [i async for i in asyncify(obj) for j in obj] |
| 232 |
reveal_type(lst2) # N: Revealed type is "builtins.list[builtins.int]" |
| 233 |
|
| 234 |
async def setcomp(obj: Iterable[int]): |
| 235 |
lst = {i async for i in asyncify(obj)} |
| 236 |
reveal_type(lst) # N: Revealed type is "builtins.set[builtins.int]" |
| 237 |
|
| 238 |
async def dictcomp(obj: Iterable[Tuple[int, str]]): |
| 239 |
lst = {i: j async for i, j in asyncify(obj)} |
| 240 |
reveal_type(lst) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 241 |
|
| 242 |
async def generatorexp(obj: Iterable[int]): |
| 243 |
lst = (i async for i in asyncify(obj)) |
| 244 |
reveal_type(lst) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 245 |
lst2 = (i async for i in asyncify(obj) for i in obj) |
| 246 |
reveal_type(lst2) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 247 |
|
| 248 |
[builtins fixtures/async_await.pyi] |
| 249 |
[typing fixtures/typing-async.pyi] |
| 250 |
|
| 251 |
[case testAsyncForComprehensionErrors] |
| 252 |
from typing import Generic, Iterable, TypeVar, AsyncIterator, Tuple |
| 253 |
|
| 254 |
T = TypeVar('T') |
| 255 |
|
| 256 |
class asyncify(Generic[T], AsyncIterator[T]): |
| 257 |
def __init__(self, iterable: Iterable[T]) -> None: |
| 258 |
self.iterable = iter(iterable) |
| 259 |
def __aiter__(self) -> AsyncIterator[T]: |
| 260 |
return self |
| 261 |
async def __anext__(self) -> T: |
| 262 |
try: |
| 263 |
return next(self.iterable) |
| 264 |
except StopIteration: |
| 265 |
raise StopAsyncIteration |
| 266 |
|
| 267 |
async def wrong_iterable(obj: Iterable[int]): |
| 268 |
[i async for i in obj] # E: "Iterable[int]" has no attribute "__aiter__" (not async iterable) |
| 269 |
[i for i in asyncify(obj)] # E: "asyncify[int]" has no attribute "__iter__"; maybe "__aiter__"? (not iterable) |
| 270 |
{i: i async for i in obj} # E: "Iterable[int]" has no attribute "__aiter__" (not async iterable) |
| 271 |
{i: i for i in asyncify(obj)} # E: "asyncify[int]" has no attribute "__iter__"; maybe "__aiter__"? (not iterable) |
| 272 |
[builtins fixtures/async_await.pyi] |
| 273 |
[typing fixtures/typing-async.pyi] |
| 274 |
|
| 275 |
[case testAsyncWith] |
| 276 |
|
| 277 |
class C: |
| 278 |
async def __aenter__(self) -> int: pass |
| 279 |
async def __aexit__(self, x, y, z) -> None: pass |
| 280 |
async def f() -> None: |
| 281 |
async with C() as x: |
| 282 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 283 |
[builtins fixtures/async_await.pyi] |
| 284 |
[typing fixtures/typing-async.pyi] |
| 285 |
|
| 286 |
|
| 287 |
[case testAsyncWithError] |
| 288 |
|
| 289 |
class C: |
| 290 |
def __enter__(self) -> int: pass |
| 291 |
def __exit__(self, x, y, z) -> None: pass |
| 292 |
async def f() -> None: |
| 293 |
async with C() as x: |
| 294 |
pass |
| 295 |
[builtins fixtures/async_await.pyi] |
| 296 |
[typing fixtures/typing-async.pyi] |
| 297 |
[out] |
| 298 |
main:6: error: "C" has no attribute "__aenter__"; maybe "__enter__"? |
| 299 |
main:6: error: "C" has no attribute "__aexit__"; maybe "__exit__"? |
| 300 |
|
| 301 |
[case testAsyncWithErrorBadAenter] |
| 302 |
|
| 303 |
class C: |
| 304 |
def __aenter__(self) -> int: pass |
| 305 |
async def __aexit__(self, x, y, z) -> None: pass |
| 306 |
async def f() -> None: |
| 307 |
async with C() as x: # E: Incompatible types in "async with" for "__aenter__" (actual type "int", expected type "Awaitable[Any]") |
| 308 |
pass |
| 309 |
[builtins fixtures/async_await.pyi] |
| 310 |
[typing fixtures/typing-async.pyi] |
| 311 |
|
| 312 |
[case testAsyncWithErrorBadAenter2] |
| 313 |
# flags: --no-strict-optional |
| 314 |
class C: |
| 315 |
def __aenter__(self) -> None: pass |
| 316 |
async def __aexit__(self, x, y, z) -> None: pass |
| 317 |
async def f() -> None: |
| 318 |
async with C() as x: # E: "None" has no attribute "__await__" |
| 319 |
pass |
| 320 |
[builtins fixtures/async_await.pyi] |
| 321 |
[typing fixtures/typing-async.pyi] |
| 322 |
|
| 323 |
[case testAsyncWithErrorBadAexit] |
| 324 |
|
| 325 |
class C: |
| 326 |
async def __aenter__(self) -> int: pass |
| 327 |
def __aexit__(self, x, y, z) -> int: pass |
| 328 |
async def f() -> None: |
| 329 |
async with C() as x: # E: Incompatible types in "async with" for "__aexit__" (actual type "int", expected type "Awaitable[Any]") |
| 330 |
pass |
| 331 |
[builtins fixtures/async_await.pyi] |
| 332 |
[typing fixtures/typing-async.pyi] |
| 333 |
|
| 334 |
[case testAsyncWithErrorBadAexit2] |
| 335 |
# flags: --no-strict-optional |
| 336 |
class C: |
| 337 |
async def __aenter__(self) -> int: pass |
| 338 |
def __aexit__(self, x, y, z) -> None: pass |
| 339 |
async def f() -> None: |
| 340 |
async with C() as x: # E: "None" has no attribute "__await__" |
| 341 |
pass |
| 342 |
[builtins fixtures/async_await.pyi] |
| 343 |
[typing fixtures/typing-async.pyi] |
| 344 |
|
| 345 |
[case testAsyncWithTypeComments_no_native_parse] |
| 346 |
|
| 347 |
class C: |
| 348 |
async def __aenter__(self) -> int: pass |
| 349 |
async def __aexit__(self, x, y, z) -> None: pass |
| 350 |
async def f() -> None: |
| 351 |
async with C() as x: # type: int |
| 352 |
pass |
| 353 |
|
| 354 |
async with C() as y, C() as z: # type: str, int # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 355 |
pass |
| 356 |
|
| 357 |
async with C() as a: # type: int, int # E: Syntax error in type annotation # N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn) |
| 358 |
pass |
| 359 |
[builtins fixtures/async_await.pyi] |
| 360 |
[typing fixtures/typing-async.pyi] |
| 361 |
|
| 362 |
[case testNoYieldFromInAsyncDef] |
| 363 |
|
| 364 |
async def f(): |
| 365 |
yield from [] |
| 366 |
async def g(): |
| 367 |
x = yield from [] |
| 368 |
[builtins fixtures/async_await.pyi] |
| 369 |
[out] |
| 370 |
main:3: error: "yield from" in async function |
| 371 |
main:5: error: "yield from" in async function |
| 372 |
|
| 373 |
[case testYieldFromNoAwaitable] |
| 374 |
|
| 375 |
from typing import Any, Generator |
| 376 |
async def f() -> str: |
| 377 |
return '' |
| 378 |
def g() -> Generator[Any, None, str]: |
| 379 |
x = yield from f() |
| 380 |
return x |
| 381 |
[builtins fixtures/async_await.pyi] |
| 382 |
[typing fixtures/typing-async.pyi] |
| 383 |
[out] |
| 384 |
main:6: error: "yield from" can't be applied to "Coroutine[Any, Any, str]" |
| 385 |
|
| 386 |
[case testAwaitableSubclass] |
| 387 |
|
| 388 |
from typing import Any, AsyncIterator, Awaitable, Generator |
| 389 |
class A(Awaitable[int]): |
| 390 |
def __await__(self) -> Generator[Any, None, int]: |
| 391 |
yield |
| 392 |
return 0 |
| 393 |
class C: |
| 394 |
def __aenter__(self) -> A: |
| 395 |
return A() |
| 396 |
def __aexit__(self, *a) -> A: |
| 397 |
return A() |
| 398 |
class I(AsyncIterator[int]): |
| 399 |
def __aiter__(self) -> 'I': |
| 400 |
return self |
| 401 |
def __anext__(self) -> A: |
| 402 |
return A() |
| 403 |
async def main() -> None: |
| 404 |
x = await A() |
| 405 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 406 |
async with C() as y: |
| 407 |
reveal_type(y) # N: Revealed type is "builtins.int" |
| 408 |
async for z in I(): |
| 409 |
reveal_type(z) # N: Revealed type is "builtins.int" |
| 410 |
[builtins fixtures/async_await.pyi] |
| 411 |
[typing fixtures/typing-async.pyi] |
| 412 |
|
| 413 |
[case testYieldTypeCheckInDecoratedCoroutine] |
| 414 |
|
| 415 |
from typing import Generator |
| 416 |
from types import coroutine |
| 417 |
@coroutine |
| 418 |
def f() -> Generator[int, str, int]: |
| 419 |
x = yield 0 |
| 420 |
x = yield '' # E: Incompatible types in "yield" (actual type "str", expected type "int") |
| 421 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 422 |
if x: |
| 423 |
return 0 |
| 424 |
else: |
| 425 |
return '' # E: Incompatible return value type (got "str", expected "int") |
| 426 |
[builtins fixtures/async_await.pyi] |
| 427 |
[typing fixtures/typing-async.pyi] |
| 428 |
|
| 429 |
-- Async generators (PEP 525), some test cases adapted from the PEP text |
| 430 |
-- --------------------------------------------------------------------- |
| 431 |
|
| 432 |
[case testAsyncGenerator] |
| 433 |
from typing import AsyncGenerator, Generator |
| 434 |
|
| 435 |
async def f() -> int: |
| 436 |
return 42 |
| 437 |
|
| 438 |
async def g() -> AsyncGenerator[int, None]: |
| 439 |
value = await f() |
| 440 |
reveal_type(value) # N: Revealed type is "builtins.int" |
| 441 |
yield value |
| 442 |
|
| 443 |
yield 'not an int' # E: Incompatible types in "yield" (actual type "str", expected type "int") |
| 444 |
# return without a value is fine |
| 445 |
return |
| 446 |
reveal_type(g) # N: Revealed type is "def () -> typing.AsyncGenerator[builtins.int, None]" |
| 447 |
reveal_type(g()) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 448 |
|
| 449 |
async def h() -> None: |
| 450 |
async for item in g(): |
| 451 |
reveal_type(item) # N: Revealed type is "builtins.int" |
| 452 |
|
| 453 |
async def wrong_return() -> Generator[int, None, None]: # E: The return type of an async generator function should be "AsyncGenerator" or one of its supertypes |
| 454 |
yield 3 |
| 455 |
|
| 456 |
[builtins fixtures/dict.pyi] |
| 457 |
[typing fixtures/typing-async.pyi] |
| 458 |
|
| 459 |
[case testAsyncGeneratorReturnIterator] |
| 460 |
from typing import AsyncIterator |
| 461 |
|
| 462 |
async def gen() -> AsyncIterator[int]: |
| 463 |
yield 3 |
| 464 |
|
| 465 |
yield 'not an int' # E: Incompatible types in "yield" (actual type "str", expected type "int") |
| 466 |
|
| 467 |
async def use_gen() -> None: |
| 468 |
async for item in gen(): |
| 469 |
reveal_type(item) # N: Revealed type is "builtins.int" |
| 470 |
|
| 471 |
[builtins fixtures/dict.pyi] |
| 472 |
[typing fixtures/typing-async.pyi] |
| 473 |
|
| 474 |
[case testAsyncGeneratorManualIter] |
| 475 |
from typing import AsyncGenerator |
| 476 |
|
| 477 |
async def genfunc() -> AsyncGenerator[int, None]: |
| 478 |
yield 1 |
| 479 |
yield 2 |
| 480 |
|
| 481 |
async def user() -> None: |
| 482 |
gen = genfunc() |
| 483 |
|
| 484 |
reveal_type(gen.__aiter__()) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 485 |
|
| 486 |
reveal_type(await gen.__anext__()) # N: Revealed type is "builtins.int" |
| 487 |
|
| 488 |
[builtins fixtures/dict.pyi] |
| 489 |
[typing fixtures/typing-async.pyi] |
| 490 |
|
| 491 |
[case testAsyncGeneratorAsend] |
| 492 |
from typing import AsyncGenerator |
| 493 |
|
| 494 |
async def f() -> None: |
| 495 |
pass |
| 496 |
|
| 497 |
async def gen() -> AsyncGenerator[int, str]: |
| 498 |
await f() |
| 499 |
v = yield 42 |
| 500 |
reveal_type(v) # N: Revealed type is "builtins.str" |
| 501 |
await f() |
| 502 |
|
| 503 |
async def h() -> None: |
| 504 |
g = gen() |
| 505 |
await g.asend(()) # E: Argument 1 to "asend" of "AsyncGenerator" has incompatible type "tuple[()]"; expected "str" |
| 506 |
reveal_type(await g.asend('hello')) # N: Revealed type is "builtins.int" |
| 507 |
|
| 508 |
[builtins fixtures/dict.pyi] |
| 509 |
[typing fixtures/typing-async.pyi] |
| 510 |
|
| 511 |
[case testAsyncGeneratorAthrow] |
| 512 |
from typing import AsyncGenerator |
| 513 |
|
| 514 |
async def gen() -> AsyncGenerator[str, int]: |
| 515 |
try: |
| 516 |
yield 'hello' |
| 517 |
except BaseException: |
| 518 |
yield 'world' |
| 519 |
|
| 520 |
async def h() -> None: |
| 521 |
g = gen() |
| 522 |
v = await g.asend(1) |
| 523 |
reveal_type(v) # N: Revealed type is "builtins.str" |
| 524 |
reveal_type(await g.athrow(BaseException)) # N: Revealed type is "builtins.str" |
| 525 |
|
| 526 |
[builtins fixtures/dict.pyi] |
| 527 |
[typing fixtures/typing-async.pyi] |
| 528 |
|
| 529 |
[case testAsyncGeneratorNoSyncIteration] |
| 530 |
from typing import AsyncGenerator |
| 531 |
|
| 532 |
async def gen() -> AsyncGenerator[int, None]: |
| 533 |
for i in [1, 2, 3]: |
| 534 |
yield i |
| 535 |
|
| 536 |
def h() -> None: |
| 537 |
for i in gen(): # E: "AsyncGenerator[int, None]" has no attribute "__iter__"; maybe "__aiter__"? (not iterable) |
| 538 |
pass |
| 539 |
|
| 540 |
[builtins fixtures/dict.pyi] |
| 541 |
[typing fixtures/typing-async.pyi] |
| 542 |
|
| 543 |
[case testAsyncGeneratorNoYieldFrom] |
| 544 |
from typing import AsyncGenerator |
| 545 |
|
| 546 |
async def f() -> AsyncGenerator[int, None]: |
| 547 |
pass |
| 548 |
|
| 549 |
async def gen() -> AsyncGenerator[int, None]: |
| 550 |
yield from f() # E: "yield from" in async function |
| 551 |
|
| 552 |
[builtins fixtures/dict.pyi] |
| 553 |
[typing fixtures/typing-async.pyi] |
| 554 |
|
| 555 |
[case testAsyncGeneratorNoReturnWithValue] |
| 556 |
from typing import AsyncGenerator |
| 557 |
|
| 558 |
async def return_int() -> AsyncGenerator[int, None]: |
| 559 |
yield 1 |
| 560 |
return 42 # E: "return" with value in async generator is not allowed |
| 561 |
|
| 562 |
async def return_none() -> AsyncGenerator[int, None]: |
| 563 |
yield 1 |
| 564 |
return None # E: "return" with value in async generator is not allowed |
| 565 |
|
| 566 |
def f() -> None: |
| 567 |
return |
| 568 |
|
| 569 |
async def return_f() -> AsyncGenerator[int, None]: |
| 570 |
yield 1 |
| 571 |
return f() # E: "return" with value in async generator is not allowed |
| 572 |
|
| 573 |
[builtins fixtures/dict.pyi] |
| 574 |
[typing fixtures/typing-async.pyi] |
| 575 |
|
| 576 |
[case testImplicitAsyncGenerator] |
| 577 |
from typing import List |
| 578 |
|
| 579 |
async def get_list() -> List[int]: |
| 580 |
return [1] |
| 581 |
|
| 582 |
async def predicate() -> bool: |
| 583 |
return True |
| 584 |
|
| 585 |
async def test_implicit_generators() -> None: |
| 586 |
reveal_type(await predicate() for _ in [1]) # N: Revealed type is "typing.AsyncGenerator[builtins.bool, None]" |
| 587 |
reveal_type(x for x in [1] if await predicate()) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 588 |
reveal_type(x for x in await get_list()) # N: Revealed type is "typing.Generator[builtins.int, None, None]" |
| 589 |
reveal_type(x for _ in [1] for x in await get_list()) # N: Revealed type is "typing.AsyncGenerator[builtins.int, None]" |
| 590 |
|
| 591 |
[builtins fixtures/dict.pyi] |
| 592 |
[typing fixtures/typing-async.pyi] |
| 593 |
|
| 594 |
|
| 595 |
-- The full matrix of coroutine compatibility |
| 596 |
-- ------------------------------------------ |
| 597 |
|
| 598 |
[case testFullCoroutineMatrix] |
| 599 |
|
| 600 |
from typing import Any, AsyncIterator, Awaitable, Generator, Iterator |
| 601 |
from types import coroutine |
| 602 |
|
| 603 |
# The various things you might try to use in `await` or `yield from`. |
| 604 |
|
| 605 |
def plain_generator() -> Generator[str, None, int]: |
| 606 |
yield 'a' |
| 607 |
return 1 |
| 608 |
|
| 609 |
async def plain_coroutine() -> int: |
| 610 |
return 1 |
| 611 |
|
| 612 |
@coroutine |
| 613 |
def decorated_generator() -> Generator[str, None, int]: |
| 614 |
yield 'a' |
| 615 |
return 1 |
| 616 |
|
| 617 |
@coroutine |
| 618 |
async def decorated_coroutine() -> int: |
| 619 |
return 1 |
| 620 |
|
| 621 |
class It(Iterator[str]): |
| 622 |
def __iter__(self) -> 'It': |
| 623 |
return self |
| 624 |
def __next__(self) -> str: |
| 625 |
return 'a' |
| 626 |
|
| 627 |
def other_iterator() -> It: |
| 628 |
return It() |
| 629 |
|
| 630 |
class Aw(Awaitable[int]): |
| 631 |
def __await__(self) -> Generator[str, Any, int]: |
| 632 |
yield 'a' |
| 633 |
return 1 |
| 634 |
|
| 635 |
def other_coroutine() -> Aw: |
| 636 |
return Aw() |
| 637 |
|
| 638 |
# The various contexts in which `await` or `yield from` might occur. |
| 639 |
|
| 640 |
def plain_host_generator() -> Generator[str, None, None]: |
| 641 |
yield 'a' |
| 642 |
x = 0 |
| 643 |
x = yield from plain_generator() |
| 644 |
x = yield from plain_coroutine() # E: "yield from" can't be applied to "Coroutine[Any, Any, int]" |
| 645 |
x = yield from decorated_generator() |
| 646 |
x = yield from decorated_coroutine() # E: "yield from" can't be applied to "AwaitableGenerator[Any, Any, int, Coroutine[Any, Any, int]]" |
| 647 |
x = yield from other_iterator() |
| 648 |
x = yield from other_coroutine() # E: "yield from" can't be applied to "Aw" |
| 649 |
|
| 650 |
async def plain_host_coroutine() -> None: |
| 651 |
x = 0 |
| 652 |
x = await plain_generator() # E: Incompatible types in "await" (actual type "Generator[str, None, int]", expected type "Awaitable[Any]") |
| 653 |
x = await plain_coroutine() |
| 654 |
x = await decorated_generator() |
| 655 |
x = await decorated_coroutine() |
| 656 |
x = await other_iterator() # E: Incompatible types in "await" (actual type "It", expected type "Awaitable[Any]") |
| 657 |
x = await other_coroutine() |
| 658 |
|
| 659 |
@coroutine |
| 660 |
def decorated_host_generator() -> Generator[str, None, None]: |
| 661 |
yield 'a' |
| 662 |
x = 0 |
| 663 |
x = yield from plain_generator() |
| 664 |
x = yield from plain_coroutine() |
| 665 |
x = yield from decorated_generator() |
| 666 |
x = yield from decorated_coroutine() |
| 667 |
x = yield from other_iterator() |
| 668 |
x = yield from other_coroutine() # E: "yield from" can't be applied to "Aw" |
| 669 |
|
| 670 |
@coroutine |
| 671 |
async def decorated_host_coroutine() -> None: |
| 672 |
x = 0 |
| 673 |
x = await plain_generator() # E: Incompatible types in "await" (actual type "Generator[str, None, int]", expected type "Awaitable[Any]") |
| 674 |
x = await plain_coroutine() |
| 675 |
x = await decorated_generator() |
| 676 |
x = await decorated_coroutine() |
| 677 |
x = await other_iterator() # E: Incompatible types in "await" (actual type "It", expected type "Awaitable[Any]") |
| 678 |
x = await other_coroutine() |
| 679 |
|
| 680 |
[builtins fixtures/async_await.pyi] |
| 681 |
[typing fixtures/typing-async.pyi] |
| 682 |
[out] |
| 683 |
|
| 684 |
[case testAsyncGenDisallowUntyped] |
| 685 |
# flags: --disallow-untyped-defs |
| 686 |
# These should not crash |
| 687 |
from typing import AsyncGenerator, Any |
| 688 |
|
| 689 |
async def f() -> AsyncGenerator[int, None]: |
| 690 |
yield 0 |
| 691 |
|
| 692 |
async def g() -> AsyncGenerator[Any, None]: |
| 693 |
yield 0 |
| 694 |
[builtins fixtures/async_await.pyi] |
| 695 |
[typing fixtures/typing-async.pyi] |
| 696 |
[out] |
| 697 |
|
| 698 |
[case testAsyncGenDisallowUntypedTriggers] |
| 699 |
# flags: --disallow-untyped-defs |
| 700 |
from typing import AsyncGenerator, Any |
| 701 |
|
| 702 |
async def f() -> AsyncGenerator[Any, Any]: |
| 703 |
yield None |
| 704 |
|
| 705 |
async def h() -> Any: |
| 706 |
yield 0 |
| 707 |
|
| 708 |
async def g(): # E: Function is missing a return type annotation |
| 709 |
yield 0 |
| 710 |
[builtins fixtures/async_await.pyi] |
| 711 |
[typing fixtures/typing-async.pyi] |
| 712 |
[out] |
| 713 |
|
| 714 |
[case testAsyncOverloadedFunction] |
| 715 |
from typing import overload |
| 716 |
|
| 717 |
@overload |
| 718 |
async def f(x: int) -> int: ... |
| 719 |
@overload |
| 720 |
async def f(x: str) -> str: ... |
| 721 |
async def f(x): |
| 722 |
pass |
| 723 |
|
| 724 |
reveal_type(f) # N: Revealed type is "Overload(def (x: builtins.int) -> typing.Coroutine[Any, Any, builtins.int], def (x: builtins.str) -> typing.Coroutine[Any, Any, builtins.str])" |
| 725 |
[builtins fixtures/async_await.pyi] |
| 726 |
[typing fixtures/typing-async.pyi] |
| 727 |
|
| 728 |
[case testAsyncForwardRefInBody] |
| 729 |
|
| 730 |
async def f() -> None: |
| 731 |
forwardref: C |
| 732 |
class C: pass |
| 733 |
|
| 734 |
def dec(x): pass |
| 735 |
|
| 736 |
@dec |
| 737 |
async def g() -> None: |
| 738 |
forwardref: C |
| 739 |
class C: pass |
| 740 |
|
| 741 |
reveal_type(f) # N: Revealed type is "def () -> typing.Coroutine[Any, Any, None]" |
| 742 |
reveal_type(g) # N: Revealed type is "Any" |
| 743 |
[builtins fixtures/async_await.pyi] |
| 744 |
[typing fixtures/typing-async.pyi] |
| 745 |
|
| 746 |
[case testAsyncDeferredAnalysis] |
| 747 |
|
| 748 |
def t() -> None: |
| 749 |
async def f() -> int: |
| 750 |
return 1 |
| 751 |
|
| 752 |
def g() -> int: |
| 753 |
return next(iter(x)) |
| 754 |
|
| 755 |
x = [1] |
| 756 |
|
| 757 |
[builtins fixtures/async_await.pyi] |
| 758 |
[typing fixtures/typing-async.pyi] |
| 759 |
|
| 760 |
[case testAsyncWithInGenericClass] |
| 761 |
from typing import Generic, AsyncContextManager, TypeVar |
| 762 |
|
| 763 |
T = TypeVar('T', str, int) |
| 764 |
|
| 765 |
class Foo(Generic[T]): |
| 766 |
async def foo(self, manager: AsyncContextManager): |
| 767 |
async with manager: |
| 768 |
pass |
| 769 |
|
| 770 |
[builtins fixtures/async_await.pyi] |
| 771 |
[typing fixtures/typing-async.pyi] |
| 772 |
|
| 773 |
[case testAwaitOverloadSpecialCase] |
| 774 |
from typing import Any, Awaitable, Iterable, overload, Tuple, List, TypeVar, Generic |
| 775 |
|
| 776 |
T = TypeVar("T") |
| 777 |
FT = TypeVar("FT", bound='Future[Any]') |
| 778 |
|
| 779 |
class Future(Awaitable[T], Iterable[T]): |
| 780 |
pass |
| 781 |
|
| 782 |
class Task(Future[T]): |
| 783 |
pass |
| 784 |
|
| 785 |
@overload |
| 786 |
def wait(fs: Iterable[FT]) -> Future[Tuple[List[FT], List[FT]]]: ... \ |
| 787 |
# E: Overloaded function signatures 1 and 2 overlap with incompatible return types \ |
| 788 |
# N: Flipping the order of overloads will fix this error |
| 789 |
@overload |
| 790 |
def wait(fs: Iterable[Awaitable[T]]) -> Future[Tuple[List[Task[T]], List[Task[T]]]]: ... |
| 791 |
def wait(fs: Any) -> Any: |
| 792 |
pass |
| 793 |
|
| 794 |
async def imprecise1(futures: Iterable[Task[Any]]) -> None: |
| 795 |
done: Any |
| 796 |
pending: Any |
| 797 |
done, pending = await wait(futures) |
| 798 |
reveal_type(done) # N: Revealed type is "Any" |
| 799 |
|
| 800 |
async def imprecise2(futures: Iterable[Awaitable[Any]]) -> None: |
| 801 |
done, pending = await wait(futures) |
| 802 |
reveal_type(done) # N: Revealed type is "builtins.list[__main__.Task[Any]]" |
| 803 |
|
| 804 |
async def precise1(futures: Iterable[Future[int]]) -> None: |
| 805 |
done, pending = await wait(futures) |
| 806 |
reveal_type(done) # N: Revealed type is "builtins.list[__main__.Future[builtins.int]]" |
| 807 |
|
| 808 |
async def precise2(futures: Iterable[Awaitable[int]]) -> None: |
| 809 |
done, pending = await wait(futures) |
| 810 |
reveal_type(done) # N: Revealed type is "builtins.list[__main__.Task[builtins.int]]" |
| 811 |
|
| 812 |
|
| 813 |
[builtins fixtures/async_await.pyi] |
| 814 |
[typing fixtures/typing-async.pyi] |
| 815 |
|
| 816 |
[case testUnusedAwaitable] |
| 817 |
# flags: --show-error-codes --enable-error-code unused-awaitable |
| 818 |
from typing import Iterable |
| 819 |
|
| 820 |
async def foo() -> None: |
| 821 |
pass |
| 822 |
|
| 823 |
class A: |
| 824 |
def __await__(self) -> Iterable[int]: |
| 825 |
yield 5 |
| 826 |
|
| 827 |
# Things with __getattr__ should not simply be considered awaitable. |
| 828 |
class B: |
| 829 |
def __getattr__(self, attr) -> object: |
| 830 |
return 0 |
| 831 |
|
| 832 |
def bar() -> None: |
| 833 |
A() # E: Value of type "A" must be used [unused-awaitable] \ |
| 834 |
# N: Are you missing an await? |
| 835 |
foo() # E: Value of type "Coroutine[Any, Any, None]" must be used [unused-coroutine] \ |
| 836 |
# N: Are you missing an await? |
| 837 |
B() |
| 838 |
|
| 839 |
[builtins fixtures/async_await.pyi] |
| 840 |
[typing fixtures/typing-async.pyi] |
| 841 |
|
| 842 |
[case testAsyncForOutsideCoroutine] |
| 843 |
async def g(): |
| 844 |
yield 0 |
| 845 |
|
| 846 |
def f() -> None: |
| 847 |
[x async for x in g()] # E: "async for" outside async function |
| 848 |
{x async for x in g()} # E: "async for" outside async function |
| 849 |
{x: True async for x in g()} # E: "async for" outside async function |
| 850 |
(x async for x in g()) |
| 851 |
async for x in g(): ... # E: "async for" outside async function |
| 852 |
|
| 853 |
[x async for x in g()] # E: "async for" outside async function |
| 854 |
{x async for x in g()} # E: "async for" outside async function |
| 855 |
{x: True async for x in g()} # E: "async for" outside async function |
| 856 |
(x async for x in g()) |
| 857 |
async for x in g(): ... # E: "async for" outside async function |
| 858 |
|
| 859 |
[builtins fixtures/async_await.pyi] |
| 860 |
[typing fixtures/typing-async.pyi] |
| 861 |
|
| 862 |
[case testAsyncWithOutsideCoroutine] |
| 863 |
class C: |
| 864 |
async def __aenter__(self): pass |
| 865 |
async def __aexit__(self, x, y, z): pass |
| 866 |
|
| 867 |
def f() -> None: |
| 868 |
async with C() as x: # E: "async with" outside async function |
| 869 |
pass |
| 870 |
|
| 871 |
async with C() as x: # E: "async with" outside async function |
| 872 |
pass |
| 873 |
|
| 874 |
[builtins fixtures/async_await.pyi] |
| 875 |
[typing fixtures/typing-async.pyi] |
| 876 |
|
| 877 |
[case testAwaitMissingNote] |
| 878 |
from typing import Generic, TypeVar, Generator, Any, Awaitable, Type |
| 879 |
|
| 880 |
class C: |
| 881 |
x: int |
| 882 |
class D(C): ... |
| 883 |
|
| 884 |
async def foo() -> D: ... |
| 885 |
def g(x: C) -> None: ... |
| 886 |
|
| 887 |
T = TypeVar("T") |
| 888 |
class Custom(Generic[T]): |
| 889 |
def __await__(self) -> Generator[Any, Any, T]: ... |
| 890 |
|
| 891 |
class Sub(Custom[T]): ... |
| 892 |
|
| 893 |
async def test(x: Sub[D], tx: Type[Sub[D]]) -> None: |
| 894 |
foo().x # E: "Coroutine[Any, Any, D]" has no attribute "x" \ |
| 895 |
# N: Maybe you forgot to use "await"? |
| 896 |
(await foo()).x |
| 897 |
foo().bad # E: "Coroutine[Any, Any, D]" has no attribute "bad" |
| 898 |
|
| 899 |
g(foo()) # E: Argument 1 to "g" has incompatible type "Coroutine[Any, Any, D]"; expected "C" \ |
| 900 |
# N: Maybe you forgot to use "await"? |
| 901 |
g(await foo()) |
| 902 |
unknown: Awaitable[Any] |
| 903 |
g(unknown) # E: Argument 1 to "g" has incompatible type "Awaitable[Any]"; expected "C" |
| 904 |
|
| 905 |
x.x # E: "Sub[D]" has no attribute "x" \ |
| 906 |
# N: Maybe you forgot to use "await"? |
| 907 |
(await x).x |
| 908 |
x.bad # E: "Sub[D]" has no attribute "bad" |
| 909 |
|
| 910 |
a: C = x # E: Incompatible types in assignment (expression has type "Sub[D]", variable has type "C") \ |
| 911 |
# N: Maybe you forgot to use "await"? |
| 912 |
b: C = await x |
| 913 |
unknown2: Awaitable[Any] |
| 914 |
d: C = unknown2 # E: Incompatible types in assignment (expression has type "Awaitable[Any]", variable has type "C") |
| 915 |
|
| 916 |
# The notes are not show for type[...] (because awaiting them will not work) |
| 917 |
tx.x # E: "type[Sub[D]]" has no attribute "x" |
| 918 |
a2: C = tx # E: Incompatible types in assignment (expression has type "type[Sub[D]]", variable has type "C") |
| 919 |
|
| 920 |
class F: |
| 921 |
def __await__(self: T) -> Generator[Any, Any, T]: ... |
| 922 |
class G(F): ... |
| 923 |
|
| 924 |
# This should not crash. |
| 925 |
x: int = G() # E: Incompatible types in assignment (expression has type "G", variable has type "int") |
| 926 |
|
| 927 |
[builtins fixtures/async_await.pyi] |
| 928 |
[typing fixtures/typing-async.pyi] |
| 929 |
|
| 930 |
[case testAsyncGeneratorExpressionAwait] |
| 931 |
from typing import AsyncGenerator |
| 932 |
|
| 933 |
async def f() -> AsyncGenerator[int, None]: |
| 934 |
async def g(x: int) -> int: |
| 935 |
return x |
| 936 |
|
| 937 |
return (await g(x) for x in [1, 2, 3]) |
| 938 |
|
| 939 |
[builtins fixtures/async_await.pyi] |
| 940 |
[typing fixtures/typing-async.pyi] |
| 941 |
|
| 942 |
[case testAwaitUnion] |
| 943 |
from typing import overload, Union |
| 944 |
|
| 945 |
class A: ... |
| 946 |
class B: ... |
| 947 |
|
| 948 |
@overload |
| 949 |
async def foo(x: A) -> B: ... |
| 950 |
@overload |
| 951 |
async def foo(x: B) -> A: ... |
| 952 |
async def foo(x): ... |
| 953 |
|
| 954 |
async def bar(x: Union[A, B]) -> None: |
| 955 |
reveal_type(await foo(x)) # N: Revealed type is "__main__.B | __main__.A" |
| 956 |
|
| 957 |
[builtins fixtures/async_await.pyi] |
| 958 |
[typing fixtures/typing-async.pyi] |
| 959 |
|
| 960 |
[case testAsyncIteratorWithIgnoredErrors] |
| 961 |
import m |
| 962 |
|
| 963 |
async def func(l: m.L) -> None: |
| 964 |
reveal_type(l.get_iterator) # N: Revealed type is "def () -> typing.AsyncIterator[builtins.str]" |
| 965 |
reveal_type(l.get_iterator2) # N: Revealed type is "def () -> typing.AsyncIterator[builtins.str]" |
| 966 |
async for i in l.get_iterator(): |
| 967 |
reveal_type(i) # N: Revealed type is "builtins.str" |
| 968 |
|
| 969 |
reveal_type(m.get_generator) # N: Revealed type is "def () -> typing.AsyncGenerator[builtins.int, None]" |
| 970 |
async for i2 in m.get_generator(): |
| 971 |
reveal_type(i2) # N: Revealed type is "builtins.int" |
| 972 |
|
| 973 |
[file m.py] |
| 974 |
# mypy: ignore-errors=True |
| 975 |
from typing import AsyncIterator, AsyncGenerator |
| 976 |
|
| 977 |
class L: |
| 978 |
async def some_func(self, i: int) -> str: |
| 979 |
return 'x' |
| 980 |
|
| 981 |
async def get_iterator(self) -> AsyncIterator[str]: |
| 982 |
yield await self.some_func(0) |
| 983 |
|
| 984 |
async def get_iterator2(self) -> AsyncIterator[str]: |
| 985 |
if self: |
| 986 |
a = (yield 'x') |
| 987 |
|
| 988 |
async def get_generator() -> AsyncGenerator[int, None]: |
| 989 |
yield 1 |
| 990 |
|
| 991 |
[builtins fixtures/async_await.pyi] |
| 992 |
[typing fixtures/typing-async.pyi] |
| 993 |
|
| 994 |
[case testAsyncIteratorWithIgnoredErrorsAndYieldFrom] |
| 995 |
from m import L |
| 996 |
|
| 997 |
async def func(l: L) -> None: |
| 998 |
reveal_type(l.get_iterator) |
| 999 |
|
| 1000 |
[file m.py] |
| 1001 |
# mypy: ignore-errors=True |
| 1002 |
from typing import AsyncIterator |
| 1003 |
|
| 1004 |
class L: |
| 1005 |
async def get_iterator(self) -> AsyncIterator[str]: |
| 1006 |
yield from ['x'] # E: "yield from" in async function |
| 1007 |
[builtins fixtures/async_await.pyi] |
| 1008 |
[typing fixtures/typing-async.pyi] |
| 1009 |
|
| 1010 |
[case testInvalidComprehensionNoCrash] |
| 1011 |
# flags: --show-error-codes |
| 1012 |
async def foo(x: int) -> int: ... |
| 1013 |
|
| 1014 |
# These are allowed in some cases: |
| 1015 |
top_level = await foo(1) # E: "await" outside function [top-level-await] |
| 1016 |
crasher = [await foo(x) for x in [1, 2, 3]] # E: "await" outside function [top-level-await] |
| 1017 |
|
| 1018 |
def bad() -> None: |
| 1019 |
# These are always critical / syntax issues: |
| 1020 |
y = [await foo(x) for x in [1, 2, 3]] # E: "await" outside coroutine ("async def") [await-not-async] |
| 1021 |
async def good() -> None: |
| 1022 |
y = [await foo(x) for x in [1, 2, 3]] # OK |
| 1023 |
[builtins fixtures/async_await.pyi] |
| 1024 |
[typing fixtures/typing-async.pyi] |
| 1025 |
|
| 1026 |
[case testNestedAsyncFunctionAndTypeVarAvalues] |
| 1027 |
from typing import TypeVar |
| 1028 |
|
| 1029 |
T = TypeVar('T', int, str) |
| 1030 |
|
| 1031 |
def f(x: T) -> None: |
| 1032 |
async def g() -> T: |
| 1033 |
return x |
| 1034 |
[builtins fixtures/async_await.pyi] |
| 1035 |
[typing fixtures/typing-async.pyi] |
| 1036 |
|
| 1037 |
[case testNestedAsyncGeneratorAndTypeVarAvalues] |
| 1038 |
from typing import AsyncGenerator, TypeVar |
| 1039 |
|
| 1040 |
T = TypeVar('T', int, str) |
| 1041 |
|
| 1042 |
def f(x: T) -> None: |
| 1043 |
async def g() -> AsyncGenerator[T, None]: |
| 1044 |
yield x |
| 1045 |
[builtins fixtures/async_await.pyi] |
| 1046 |
[typing fixtures/typing-async.pyi] |
| 1047 |
|
| 1048 |
[case testNestedDecoratedCoroutineAndTypeVarValues] |
| 1049 |
from typing import Generator, TypeVar |
| 1050 |
from types import coroutine |
| 1051 |
|
| 1052 |
T = TypeVar('T', int, str) |
| 1053 |
|
| 1054 |
def f(x: T) -> None: |
| 1055 |
@coroutine |
| 1056 |
def inner() -> Generator[T, None, None]: |
| 1057 |
yield x |
| 1058 |
reveal_type(inner) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]" \ |
| 1059 |
# N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.str, None, None, typing.Generator[builtins.str, None, None]]" |
| 1060 |
|
| 1061 |
@coroutine |
| 1062 |
def coro() -> Generator[int, None, None]: |
| 1063 |
yield 1 |
| 1064 |
reveal_type(coro) # N: Revealed type is "def () -> typing.AwaitableGenerator[builtins.int, None, None, typing.Generator[builtins.int, None, None]]" |
| 1065 |
[builtins fixtures/async_await.pyi] |
| 1066 |
[typing fixtures/typing-async.pyi] |
| 1067 |
|
| 1068 |
[case asyncIteratorInProtocol] |
| 1069 |
from typing import AsyncIterator, Protocol |
| 1070 |
|
| 1071 |
class P(Protocol): |
| 1072 |
async def launch(self) -> AsyncIterator[int]: |
| 1073 |
raise BaseException |
| 1074 |
|
| 1075 |
class Launcher(P): |
| 1076 |
def launch(self) -> AsyncIterator[int]: # E: Return type "AsyncIterator[int]" of "launch" incompatible with return type "Coroutine[Any, Any, AsyncIterator[int]]" in supertype "P" \ |
| 1077 |
# N: Consider declaring "launch" in supertype "P" without "async" \ |
| 1078 |
# N: See https://mypy.readthedocs.io/en/stable/more_types.html#asynchronous-iterators |
| 1079 |
raise BaseException |
| 1080 |
|
| 1081 |
[builtins fixtures/async_await.pyi] |
| 1082 |
[typing fixtures/typing-async.pyi] |
| 1083 |
|
| 1084 |
[case testInvalidAsyncForInComprehensionNoCrash] |
| 1085 |
# a # is at the end to mark these not as section headers |
| 1086 |
[[5 async for _ in [5]] for _ in []]# # E: "async for" outside async function \ |
| 1087 |
# E: "list[int]" has no attribute "__aiter__" (not async iterable) |
| 1088 |
[{5 async for _ in [5]} for _ in []]# # E: "async for" outside async function \ |
| 1089 |
# E: "list[int]" has no attribute "__aiter__" (not async iterable) |
| 1090 |
[{5: 5 async for _ in [5]} for _ in []]# # E: "async for" outside async function \ |
| 1091 |
# E: "list[int]" has no attribute "__aiter__" (not async iterable) |
| 1092 |
[builtins fixtures/async_await.pyi] |
| 1093 |
[typing fixtures/typing-async.pyi] |
| 1094 |
|
| 1095 |
[case testTypesCoroutineDecoratorUntyped] |
| 1096 |
import types |
| 1097 |
|
| 1098 |
@types.coroutine |
| 1099 |
def f(x): |
| 1100 |
yield |
| 1101 |
1 + "" # OK, in untyped function |
| 1102 |
return 1 |
| 1103 |
|
| 1104 |
async def test() -> None: |
| 1105 |
reveal_type(f) # N: Revealed type is "def (x: Any) -> typing.AwaitableGenerator[Any, Any, Any, Any]" |
| 1106 |
reveal_type(await f(1)) # N: Revealed type is "Any" |
| 1107 |
|
| 1108 |
[builtins fixtures/async_await.pyi] |
| 1109 |
[typing fixtures/typing-async.pyi] |
| 1110 |
|
| 1111 |
[case testTypesCoroutineDecoratorPartiallyTyped] |
| 1112 |
import types |
| 1113 |
|
| 1114 |
@types.coroutine |
| 1115 |
def f(x: int): |
| 1116 |
yield |
| 1117 |
1 + "" # E: Unsupported left operand type for + ("int") |
| 1118 |
return 1 |
| 1119 |
|
| 1120 |
async def test() -> None: |
| 1121 |
reveal_type(f) # N: Revealed type is "def (x: builtins.int) -> typing.AwaitableGenerator[Any, Any, Any, Any]" |
| 1122 |
reveal_type(await f(1)) # N: Revealed type is "Any" |
| 1123 |
|
| 1124 |
[builtins fixtures/async_await.pyi] |
| 1125 |
[typing fixtures/typing-async.pyi] |