Skip to content

Allow an attribute declared in a superclass's class body to be overridden by an attribute redeclared in a subclass's __init__ method #1345

@AlexWaygood

Description

@AlexWaygood

Summary

Consider the following snippet. Ty, mypy, pyright and pyrefly all agree on the type of B1().x, but ty disagrees with the other three on the type of B2().x:

class A:
    x: int

class B1(A):
    x: bool

class B2(A):
    def __init__(self) -> None:
        self.x: bool = True


reveal_type(B1().x)  # Ty, mypy, pyright, pyrefly: bool
reveal_type(B2().x)  # Mypy, pyright, pyrefly: bool. Ty: int!

I think for compatibility, it would be better if we followed what the other type checkers do here.

I spotted this by looking at the ecosystem report in astral-sh/ruff#20723 (comment) (all 4 new unresolved-attribute errors on aiohttp in the ecosystem report are caused by this).

Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    attribute accessInstance attributes, class attributes, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions