-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy does not handle __class_getitem__ #11501
Comments
Oh wow! I always thought it was possible! PR with the fix is on its way. I hope, that I won't break things. |
Ok, I misunderstood your problem a bit at first. Please, let me explain what is going on. class A:
def __class_getitem__(cls, item):
return cls
x = A[int] Here Mypy here complains about missing What is your use-case for bare |
I was trying to implement a generic-like interface indexed with strings. Something like:
where new types are constructed at run time based on the string. If I’m not wrong, this isn’t possible with Generic as a base class, since the index isn’t a type. Is |
I should add: type checking also breaks if using a metaclass with |
This might be a good idea! I will try to send a prototype PR today. We can start a further discussion from there. |
It might be a good idea for this to be fixed in mypy, as the language doesn't prevent you from using It may be worth noting, however, that the documentation for
|
@AlexWaygood see my #11558 PR, it has lots of problems. I am not sure that this is actually worth the effort. |
@jayanthkoushikim in what way does "type checking also break if using a metaclass with from typing import TypeVar, Any
T = TypeVar('T')
class Meta(type):
def __getitem__(cls: type[T], arg: Any) -> type[T]:
return cls
class Foo(metaclass=Meta): ...
reveal_type(Foo["Does this work?"]) # Revealed type is "Type[__main__.Foo*]" |
@AlexWaygood Hm, interesting that reveal type works. But assigning
|
@jayanthkoushik fair enough! |
IMO, at least |
Yes, I think I agree that this would be very nice to have in the case of metaclass |
It looks like class Meta(type):
def __getitem__(cls, arg: str) -> 'Meta':
return cls
class My(metaclass=Meta):
...
reveal_type(My[1])
# out/ex.py:8: note: Revealed type is "ex.Meta"
# out/ex.py:8: error: Invalid index type "int" for "Type[My]"; expected type "str" |
To add a case to this, it would be cool if we could leverage a custom from typing import ClassVar, Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
cls_attr: ClassVar[int]
def __class_getitem__(cls, item: tuple[int, T]):
cls.cls_attr = item[0]
return super().__class_getitem__(item[1])
def __init__(self, arg: T):
self.arg = arg
foo = Foo[1, bool](arg=True)
assert foo.cls_attr == 1 |
I have created a library to generate partial models for pydantic (https://github.com/team23/pydantic-partial). The basic idea would be to have something like Currently I am using a function to generate the partial model instead of As an additional note: I'm totally aware that I am generating dynamic types here which will pose additional headaches. I'm currently in the progress of investigating how to "tell" mypy that my current approach creates a valid type (error like: "Unsupported dynamic base class"). But this is a totally different story and may be fixable with creating a mypy plugin (I'm possibly going for this, soon). I just stumbled over this issue and thought to add this idea here - as I really would like |
I'm sorry that I'm asking here, but the question I want to ask is strictly connected to the subject of this issue. Correct me if I'm wrong, but from what I understood, Now question is does it concern generic core abstract classes as well? Because I have this feature request #14537 in which I have given an example for which mypy is returning errors: multi.py:26: error: Incompatible types in assignment (expression has type "List[str]", variable has type "MyIterable[str]") [assignment]
multi.py:27: error: Incompatible types in assignment (expression has type "List[List[str]]", variable has type "MyIterable[MyIterable[str]]") [assignment]
multi.py:40: error: Argument 3 to "get_enum_str" has incompatible type "List[str]"; expected "MyIterable[str]" [arg-type]
multi.py:41: error: Argument 2 to "get_enum_str_list" has incompatible type "List[str]"; expected "MyIterable[str]" [arg-type]
multi.py:41: error: Argument 3 to "get_enum_str_list" has incompatible type "List[List[str]]"; expected "MyIterable[MyIterable[str]]" [arg-type]
multi.py:42: error: Argument 2 to "get_enum_str_list" has incompatible type "str"; expected "MyIterable[str]" [arg-type]
multi.py:42: error: Argument 3 to "get_enum_str_list" has incompatible type "List[List[str]]"; expected "MyIterable[MyIterable[str]]" [arg-type] |
PEP 560 defines
__class_getitem__
as a way to enable indexing a class (to define custom generic types for instance), butmypy
does not seem to recognize this.To Reproduce
Expected Behavior
Actual Behavior
Environment
The text was updated successfully, but these errors were encountered: