Skip to content

Type alias as a class member is not valid as a type #7866

Closed
@simsa-st

Description

@simsa-st

I am not sure if this is a bug or intended behaviour but I have problems when using type aliases as class members. This is the code I thought should work:

from typing import NamedTuple

class A:
    Alias = NamedTuple("Alias", [("field", str)])
    def f(self, x: Alias) -> str: return x.field

class B:
    Alias = A.Alias
    def f(self, x: Alias) -> str: return x.field

gives the following errors:

mypy_alias.py:9: error: Variable "mypy_alias.B.Alias" is not valid as a type
mypy_alias.py:9: error: Alias? has no attribute "field"

I noticed this simpler code also does not work, I am not sure if it is connected?

class A:
    Alias = str
    def f(self, x: Alias) -> str: return x

This gives: mypy_alias.py:3: error: Variable "mypy_alias.A.Alias" is not valid as a type

I am using python 3.6.9 and mypy 0.730

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