Skip to content

metaclasses implementing __getitem__ work only in reveal_type(), cannot be used anywhere #15107

Closed
@raphCode

Description

@raphCode

Bug Report

#2827 added support for metaclasses implementing __getitem__(). This can be used for custom generic classes, without inheriting from Generic.
While the functionality is working in reveal_type(), it cannot be used anywhere, since that throws errors again. This renders custom generics unuseable at the moment.

To Reproduce

I took the test case from the PR and added two lines afterwards that try to use the custom generic class.

Playground

from typing import *

class M(type):
    def __getitem__(self, key) -> int: return 1

class A(metaclass=M): pass

reveal_type(A[M])

t: TypeAlias = A[M]
i: A[M] = 2

Expected Behavior

No errors.

Actual Behavior

main.py:8: note: Revealed type is "builtins.int"
main.py:10: error: "A" expects no type arguments, but 1 given  [type-arg]
main.py:11: error: "A" expects no type arguments, but 1 given  [type-arg]
main.py:11: error: Incompatible types in assignment (expression has type "int", variable has type "A")  [assignment]

Your Environment

  • Mypy version used: 1.2.0
  • Python version used: 3.11

Additional Comments:
This was already reported previously:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions