[Python] Python typing is rejecting override of a property with a child of its type

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Here is an example of my issue:

    class Foo:
    pass

    class FooChild(Foo):
    pass

    class Bar:
    def __init__(self):
    self.prop: Foo

    class BarChild(Bar):
    def __init__(self):
    self.prop: FooChild = FooChild()


    Here I have a warning on the override of the prop property:

    "prop" overrides symbol of same name in class "Bar"
    Variable is mutable so its type is invariant
    Override type "FooChild" is not the same as base type "Foo"


    I am confused as to why this is an error. A child would implement its parent, so why can I not override it in this case?

    Continue reading...

Compartilhe esta Página