Closed
Description
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.
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:
- mypy does not handle __class_getitem__ #11501 (comment) (did not get any attention)
- Is it possible to define a subscriptable class which can take type arguments and ellipses? #8414 (the underlying problem was probably not worked out enough)