KEMBAR78
TypeAlias cannot be used as a metaclass · Issue #13334 · python/mypy · GitHub
Skip to content

TypeAlias cannot be used as a metaclass #13334

@sobolevn

Description

@sobolevn

Here's a quick sample of a problem:

from typing_extensions import TypeAlias

Explicit: TypeAlias = type
Implicit = type

class E(metaclass=Explicit): ...  # Invalid metaclass "Explicit"
class I(metaclass=Implicit): ...  # Invalid metaclass "Implicit"

I think that using type aliases should be allowed in metaclasses.

Why?

  1. This blocks conditional metaclasses in typeshed, see Add missing metaclasses typeshed#8497

For example, see dataclasses.pyi definition:

if sys.version_info >= (3, 9):
    _InitVarMeta = type
else:
    class _InitVarMeta(type): ...  # whatever methods
  1. It is valid in runtime
  2. I don't think that unaliasing an alias here can lead to some type-checking problems (yes-yes, we have recursive type-aliases and what not 😒)

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongsemantic-analyzerProblems that happen during semantic analysistopic-metaclassestopic-type-aliasTypeAlias and other type alias issues

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions