Skip to content

Type aliases in class definitions #9238

@evhub

Description

@evhub

Under MyPy 0.780, the code

from typing import List

class ABC:
    ABCInputType = List

x: ABC.ABCInputType = []

throws

test.py:6: error: Variable "test.ABC.ABCInputType" is not valid as a type
test.py:6: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

while

from typing import List

ABCInputType = List

x: ABCInputType = []

works just fine.

The use of type aliases in classes seems both like a pretty legitimate use case to me (consider for example the ability to have an input type for the class as an attribute of the class as above) and also seems consistent with both the typing documentation and mypy's documentation, neither of which seem to mention that type aliases can only appear at the top level (only that they need to be plain assignments, as they are in the above code).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions