Skip to content
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

TypeAlias cannot be used as a metaclass #13334

Closed
sobolevn opened this issue Aug 5, 2022 · 0 comments · Fixed by #13335
Closed

TypeAlias cannot be used as a metaclass #13334

sobolevn opened this issue Aug 5, 2022 · 0 comments · Fixed by #13335
Assignees
Labels
bug mypy got something wrong semantic-analyzer Problems that happen during semantic analysis topic-metaclasses topic-type-alias TypeAlias and other type alias issues

Comments

@sobolevn
Copy link
Member

sobolevn commented Aug 5, 2022

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 😒)
@sobolevn sobolevn added the bug mypy got something wrong label Aug 5, 2022
@sobolevn sobolevn self-assigned this Aug 5, 2022
@sobolevn sobolevn added the semantic-analyzer Problems that happen during semantic analysis label Aug 5, 2022
@AlexWaygood AlexWaygood added topic-type-alias TypeAlias and other type alias issues topic-metaclasses labels Aug 5, 2022
sobolevn added a commit to sobolevn/mypy that referenced this issue Aug 5, 2022
sobolevn added a commit that referenced this issue Aug 5, 2022
* Support type aliases in metaclasses, refs #13334

* More tests

* Address review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong semantic-analyzer Problems that happen during semantic analysis topic-metaclasses topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants