Last active
January 1, 2022 17:22
-
-
Save Gobot1234/139c97c5ad8c58e75442f64e759945a9 to your computer and use it in GitHub Desktop.
test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from typing import Any, Awaitable, Callable, TypeVar, cast, Generic | |
import asyncio | |
T = TypeVar("T", bound=int, covariant=True) | |
B = TypeVar("B", bound="Box[Any]") | |
class Box(Generic[T]): ... | |
async def foo( | |
check: Callable[[B], bool] | None = None, | |
) -> B: | |
return ( | |
await bar(check) | |
if check is not None | |
else cast(B, Box()) | |
) | |
def bar(check: Callable[[B], bool]) -> Awaitable[B]: | |
... | |
my_foo: Box[int] = asyncio.run(foo()) | |
reveal_type(my_foo) # revealed type is B@foo not Box[int] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from typing import Any, Awaitable, Callable, TypeVar, cast, Generic | |
import asyncio | |
T = TypeVar("T", bound=int, covariant=True) | |
B = TypeVar("B", bound="Box[Any]") | |
class Box(Generic[T]): ... | |
def foo( | |
check: Callable[[B], bool] | None = None, | |
) -> B: | |
return ( | |
bar(check) | |
if check is not None | |
else cast(B, Box()) | |
) | |
def bar(check: Callable[[B], bool]) -> B: | |
... | |
my_foo: Box[int] = foo() | |
reveal_type(my_foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment