Skip to content

Possible to specify generic typed class via its __init__()? #1084

Answered by BvB93
abelcheung asked this question in Q&A
Discussion options

You must be logged in to vote

An option here would be to overload __init__ w.r.t. boolean literals:

class Klass(Generic[_T]):
    @overload
    def __init__(self: Klass[int], is_int: Literal[True]) -> None: ...
    @overload
    def __init__(self: Klass[str], is_int: Literal[False]) -> None: ...

Replies: 1 comment 2 replies

Comment options

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

@erictraut
Comment options

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