[Python] MyPy says 'Incompatible types in assignment' when assigning from multiple inheritance

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    Here is a sample:

    class A: pass

    class B: pass

    class X(A, B): pass

    class Y(A, B): pass

    z: B = Y() if temp else X() # MyPy error


    The error is Incompatible types in assignment (expression has type "A", variable has type "B").

    I could write my method like this:

    def func(temp: bool):
    x: B = X()
    y: B = Y()
    w: B = x if temp else y


    But is there a better solution?

    Continue reading...

Compartilhe esta Página