[Python] Discriminated union over instance attributes

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    Suppose I have the following class

    class MyClass:
    def __init__(self, ...):
    self.attr1: Union[Attr11, Attr12]
    self.attr2: Literal["val1", "val2", "val3"]
    self.attr3: Attr3
    ...
    self.attrn: Attrn


    I am interested in the following type hint:

    if self.attr2 == "val1":
    # self.attr1 should be Attr11 here
    else:
    # self.attr1 should be Attr12 here


    How can I do it?

    Continue reading...

Compartilhe esta Página