Skip to content

Protocol for callable argument that can be sync, async, or generator #1520

Answered by Daverball
rmartin16 asked this question in Q&A
Discussion options

You must be logged in to vote

You could use a Union instead of using overload, since the return type is always the same, and TypeAlias to keep things readable:

SyncHandler: TypeAlias = Callable[[str], bool]
AsyncHandler: TypeAlias = Callable[[str], Awaitable[bool]]
GenHandler: TypeAlias = Callable[[str], Generator[Any, None, bool]]

def run_handler(handler: SyncHandler | AsyncHandler | GenHandler) -> bool:
    return run_handler_magic(handler)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Daverball
Comment options

@Daverball
Comment options

Answer selected by rmartin16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants