Skip to content

Class attribute that duplicates a base class attribute is not flagged #1338

Closed

Description

@gvanrossum

The following is not an error currently:

class C:
    a = None  # type: int
class D(C):
    a = None  # type: int  # No error

(Even though repeating the definition in C does cause an error.)
Worse, there's not even a check that the types match:

class C:
    a = None  # type: int
class D(C):
    a = None  # type: str  # Still no error

Activity

  1. JukkaL commented on Apr 7, 2016

    @JukkaL
    Collaborator

    Mypy should verify that class attributes are compatible with any definitions in a base class. The latter is clearly a bug. The first one should be disallowed as well, for consistency with other variable definitions. However, just an assignment without a declared type should be okay even if the attribute is defined in a base class, but mypy should verify that the assigned value is compatible with the base class definition.

  2. gvanrossum commented on Apr 7, 2016

    @gvanrossum
    MemberAuthor

    I wonder if it's worth a note or warning if you redefine an attribute (with the same type) in a subclass? When seen as documentation, I found this confusing.

  3. added this to the milestone on Apr 7, 2016
  4. gvanrossum commented on Jan 27, 2017

    @gvanrossum
    MemberAuthor

    Fixed by #2510.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions