| -- Test cases for the default plugin | |
| -- | |
| -- Note that we have additional test cases in pythoneval.test (that use real typeshed stubs). | |
| [case testContextManagerWithGenericFunction] | |
| from contextlib import contextmanager | |
| from typing import TypeVar, Iterator | |
| T = TypeVar('T') | |
| @contextmanager | |
| def yield_id(item: T) -> Iterator[T]: | |
| yield item | |
| reveal_type(yield_id) # N: Revealed type is "def [T] (item: T`-1) -> contextlib.GeneratorContextManager[T`-1]" | |
| with yield_id(1) as x: | |
| reveal_type(x) # N: Revealed type is "builtins.int" | |
| f = yield_id | |
| def g(x, y): pass | |
| f = g # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Callable[[T], GeneratorContextManager[T]]") | |
| [typing fixtures/typing-medium.pyi] | |
| [builtins fixtures/tuple.pyi] | |
| [case testAsyncContextManagerWithGenericFunction] | |
| # flags: --python-version 3.7 | |
| from contextlib import asynccontextmanager | |
| from typing import TypeVar, AsyncGenerator | |
| T = TypeVar('T') | |
| @asynccontextmanager | |
| async def yield_id(item: T) -> AsyncGenerator[T, None]: | |
| yield item | |
| reveal_type(yield_id) # N: Revealed type is "def [T] (item: T`-1) -> typing.AsyncContextManager[T`-1]" | |
| async def f() -> None: | |
| async with yield_id(1) as x: | |
| reveal_type(x) # N: Revealed type is "builtins.int" | |
| [typing fixtures/typing-async.pyi] | |
| [builtins fixtures/tuple.pyi] | |
| [case testContextManagerReturnsGenericFunction] | |
| import contextlib | |
| from typing import Callable | |
| from typing import Generator | |
| from typing import Iterable | |
| from typing import TypeVar | |
| TArg = TypeVar('TArg') | |
| TRet = TypeVar('TRet') | |
| @contextlib.contextmanager | |
| def _thread_mapper(maxsize: int) -> Generator[ | |
| Callable[[Callable[[TArg], TRet], Iterable[TArg]], Iterable[TRet]], | |
| None, None, | |
| ]: | |
| # defined inline as there isn't a builtins.map fixture | |
| def my_map(f: Callable[[TArg], TRet], it: Iterable[TArg]) -> Iterable[TRet]: | |
| for x in it: | |
| yield f(x) | |
| yield my_map | |
| def identity(x: int) -> int: return x | |
| with _thread_mapper(1) as m: | |
| lst = list(m(identity, [2, 3])) | |
| reveal_type(lst) # N: Revealed type is "builtins.list[builtins.int]" | |
| [typing fixtures/typing-medium.pyi] | |
| [builtins fixtures/list.pyi] | |
| [case testContextManagerWithUnspecifiedArguments] | |
| from contextlib import contextmanager | |
| from typing import Callable, Iterator | |
| c: Callable[..., Iterator[int]] | |
| reveal_type(c) # N: Revealed type is "def (*Any, **Any) -> typing.Iterator[builtins.int]" | |
| reveal_type(contextmanager(c)) # N: Revealed type is "def (*Any, **Any) -> contextlib.GeneratorContextManager[builtins.int]" | |
| [typing fixtures/typing-medium.pyi] | |
| [builtins fixtures/tuple.pyi] |