Skip to content

Dynamic method signature, depending on init args #1494

Answered by henribru
Redoubts asked this question in Q&A
Discussion options

You must be logged in to vote

Oh yes, actually it does, but the bound needs to be Literal[True, False]

from typing import Literal, overload, TypeVar, Generic

Grouped = TypeVar("Grouped", bound=Literal[True, False])

class X(Generic[Grouped]):
    def __init__(self, grouped: Grouped) -> None: pass

x1 = X(True)
reveal_type(x1) # __main__.X[Literal[True]]

x2 = X(False)
reveal_type(x2) # __main__.X[Literal[False]]

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@henribru
Comment options

@henribru
Comment options

Answer selected by Redoubts
@Redoubts
Comment options

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