Created
September 17, 2019 10:31
-
-
Save asvetlov/331568fce06cc473b0872118976e92cf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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