Skip to content

Type declarations of base class attributes not carried over to subclasses if None is present #2022

Closed
@Vgr255

Description

@Vgr255

Simplest repro:

class BaseClass:
    my_attr = () # type: tuple

class Subclass(BaseClass):
    my_attr = None

Output:

testing.py: note: In class "Subclass":
testing.py:5: error: Need type annotation for variable

However, if None isn't the sole value but part of something else, this is likely not the desired behaviour. Consider this:

from typing import Tuple, Any

class BaseClass:
    my_attr = ( ("", None), ) # type: Tuple[Tuple[str, Any]]

class Subclass(BaseClass):
    my_attr = (
        ("one",  set ),
        ("two",  str ),
        ("three",None),
    )

Output:

testing.py: note: In class "Subclass":
testing.py:8: error: Need type annotation for variable

Changing None for anything else, adding a # type: ignore comment on the ("one", set), line (but not on any other line of the assignment) silences the error. This is somewhat related to #1338 (and, to a lesser extent, #1032).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions