Skip to content

Superclass fields incorrectly assumed to exist #2877

Closed
@marcoeilers

Description

@marcoeilers

Mypy seems to always assume that classes have all fields defined in their superclass' __init__ method, even if their own __init__ never calls the superclass constructor and those fields therefore don't exist.

class A:
    def __init__(self) -> None:
        self.a = 12


class B(A):
    def __init__(self) -> None:
        self.b = 14


def m() -> int:
    ab = B()
    return ab.a + ab.b  # accepted by mypy, but fails at runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions