Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Created September 17, 2019 10:31
Show Gist options
  • Save asvetlov/331568fce06cc473b0872118976e92cf to your computer and use it in GitHub Desktop.
Save asvetlov/331568fce06cc473b0872118976e92cf to your computer and use it in GitHub Desktop.
from typing import *
class RawStream:
pass
class Stream:
pass
class Other:
pass
_T = TypeVar('_T')
Factory = Callable[[RawStream], Awaitable[_T]]
async def default(raw: RawStream) -> Stream:
return Stream()
async def none(raw: RawStream) -> RawStream:
return raw
async def other(raw: RawStream) -> Other:
return Other()
@overload
async def connect() -> Stream: ...
@overload
async def connect(factory: Factory[_T]) -> _T: ...
async def connect(factory=default): # type: ignore
raw = RawStream()
return await factory(raw)
async def test() -> None:
s: Stream = await connect()
r: RawStream = await connect(none)
o = await connect(other)
reveal_type(o)
await connect(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment