Skip to content

Support type aliases in metaclasses #13335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,15 +1985,27 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
if isinstance(sym.node, PlaceholderNode):
self.defer(defn)
return
if not isinstance(sym.node, TypeInfo) or sym.node.tuple_type is not None:

# Support type aliases, like `_Meta: TypeAlias = type`
if (
isinstance(sym.node, TypeAlias)
and sym.node.no_args
and isinstance(sym.node.target, ProperType)
and isinstance(sym.node.target, Instance)
):
metaclass_info: Optional[Node] = sym.node.target.type
else:
metaclass_info = sym.node

if not isinstance(metaclass_info, TypeInfo) or metaclass_info.tuple_type is not None:
self.fail(f'Invalid metaclass "{metaclass_name}"', defn.metaclass)
return
if not sym.node.is_metaclass():
if not metaclass_info.is_metaclass():
self.fail(
'Metaclasses not inheriting from "type" are not supported', defn.metaclass
)
return
inst = fill_typevars(sym.node)
inst = fill_typevars(metaclass_info)
assert isinstance(inst, Instance)
defn.info.declared_metaclass = inst
defn.info.metaclass_type = defn.info.calculate_metaclass_type()
Expand Down
51 changes: 51 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -4486,6 +4486,57 @@ class A(metaclass=M):
reveal_type(A.y) # N: Revealed type is "builtins.int"
A.x # E: "Type[A]" has no attribute "x"

[case testValidTypeAliasAsMetaclass]
from typing_extensions import TypeAlias

Explicit: TypeAlias = type
Implicit = type

class E(metaclass=Explicit): ...
class I(metaclass=Implicit): ...
[builtins fixtures/classmethod.pyi]

[case testValidTypeAliasOfTypeAliasAsMetaclass]
from typing_extensions import TypeAlias

Explicit: TypeAlias = type
Implicit = type

A1: TypeAlias = Explicit
A2 = Explicit
A3: TypeAlias = Implicit
A4 = Implicit

class C1(metaclass=A1): ...
class C2(metaclass=A2): ...
class C3(metaclass=A3): ...
class C4(metaclass=A4): ...
[builtins fixtures/classmethod.pyi]

[case testTypeAliasWithArgsAsMetaclass]
from typing import Generic, TypeVar
from typing_extensions import TypeAlias

T = TypeVar('T')
class Meta(Generic[T]): ...

Explicit: TypeAlias = Meta[T]
Implicit = Meta[T]

class E(metaclass=Explicit): ... # E: Invalid metaclass "Explicit"
class I(metaclass=Implicit): ... # E: Invalid metaclass "Implicit"
[builtins fixtures/classmethod.pyi]

[case testTypeAliasNonTypeAsMetaclass]
from typing_extensions import TypeAlias

Explicit: TypeAlias = int
Implicit = int

class E(metaclass=Explicit): ... # E: Metaclasses not inheriting from "type" are not supported
class I(metaclass=Implicit): ... # E: Metaclasses not inheriting from "type" are not supported
[builtins fixtures/classmethod.pyi]

[case testInvalidVariableAsMetaclass]
from typing import Any
M = 0 # type: int
Expand Down