Skip to content

Instantly share code, notes, and snippets.

@Gobot1234
Last active January 1, 2022 17:22
Show Gist options
  • Save Gobot1234/139c97c5ad8c58e75442f64e759945a9 to your computer and use it in GitHub Desktop.
Save Gobot1234/139c97c5ad8c58e75442f64e759945a9 to your computer and use it in GitHub Desktop.
test.py
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]
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