Skip to content

Type override for attributes is not checked #2211

Closed
@dmoisset

Description

@dmoisset

from what I see, overriding an attribute with an incompatible type (which can lead to unsoundness) does not produce any kind of warning with mypy:

class Base:
    attr = "foo"

    def f(self) -> str:
        return self.attr

class Derived(Base):
    attr = 42

d = Derived()

reveal_type(d.f())

The code above shows that mypy thinks that the d.f() expression is a string, while it's actually an int. Shouldn't this kind of redefinitions be forbidden?

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