Skip to content

Detect undefined attribute in child class (when defined in parent) #9225

Open
@macfreek

Description

@macfreek

mypy does not detect an undefined attribute in a child class if it is defined in a parent class.

Given the following code, where the __init__ of a child class forgets to call super().__init__:

class Base():
    def __init__(self, x: int) -> None:
        self.x = x
    def whatsx(self) -> int:
        return self.x

class Child(Base):
    def __init__(self, x: int) -> None:
        pass
        # Note: no call to super(), nor defining self.x.

c = Child(3)
print(c.whatsx())  # mypy does not catch undefined c.x

The real issue is obviously that the child class does not define the x attribute, failing Liskov.

Perhaps this is out-of-scope for mypy, but since programmers make stupid mistakes like this (/me raises hand), it would be awesome if mypy could warn about this error, making said programmers look less stupid ;)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions