-
-
Notifications
You must be signed in to change notification settings - Fork 30.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
Implement __class_getitem__ for Future, Task, Queue #83159
Comments
Typeshed declares asyncio.Future, asyncio.Task and asyncio.Queue as generic types, which is 100% correct. The problem is that these classes don't support generic instantiation in runtime, e.g. Future[str] raises TypeError. The feature should be implemented by adding __class_getitem__ methods which return self. The patch is trivial but requires a few lines of C code for C Accelerated CTask and CFuture as well as updating Python code. A volunteer is welcome! |
In principle I think this is a good idea. If this was user code, we would inherit from typing.Generic. I understand you wanting to avoid the overhead of that. But will it work correctly at runtime with other typing constructors? I guess __annotations__ will not show the generic type's parameter. Will anybody care? Maybe we need to walk through some detailed examples. |
I thought __class_getitem__ was invented exactly to simplify generic types support. The only thing that confuses me an example from PEP-560: class MyList:
def __getitem__(self, index):
return index + 1
def __class_getitem__(cls, item):
return f"{cls.__name__}[{item.__name__}]" It prevents from instantiating a generic on inheritance, e.g. the following code raises TypeError: class MyOtherList(MyList[int]):
pass It would be nice if Ivan clarifies what is the best practice in this case. |
Well, __class_getitem__ was added primarily to allow classes implemented in But I'll leave the rest of this to Ivan. |
I see the point in using |
I want to be a volunteer, if no one is working on this. |
I'm actually missing context, so I will leave this up to you all. |
Done. |
Thanks, Batuhan! |
Yes, it looks like they are using that in typeshed. I'm opening another issue with a patch. Thanks for reminding that |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: