-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add __orig_bases__ to Generic #7827
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
Conversation
This comment has been minimized.
This comment has been minimized.
Not sure this will make a difference to any type checkers. |
According to mypy_primer, this change has no effect on the checked open source code. π€π |
Hmm, experimenting with pyright I don't see a difference locally with this change. If I try swapping to class and doing class Generic:
__orig_bases__: ClassVar[tuple[type, ...]] It does look like pyright uses it. But downside is it causes other aspects of generic to no longer work. Alternative that's safer for type checkers is add |
I'm fine with this if we can figure out a way to add it to |
I'm unsure how to make Generic stub declare this. It looks like each type checker special cases generic in ways that makes me unclear if it's possible to make them happy. Alternative approach I experimented with was, class Generic:
def __class_getitem__(cls, params: Any) -> Any: ...
__orig_bases__: tuple[type, ...] This approach breaks pyright's type variable binding logic. It does make I also tested adding it to _SpecialForm. That looks to be a noop and does nothing. The current approach of _GenericSpecialForm also looks to be a noop. Amusingly deleting Generic entirely does affect type checkers and causes mypy to crash (pyright runs, with many thousands of errors). My guess is to add it to Generic will require each type checker to add some logic for this special case as they likely all have non standard handling for Generic as a type stub. Unsure it's worthwhile doing a feature request to each type checker to handle I do agree this use case is pretty niche though and my guess is if type lie isn't worth it is probably better to close pr. edit: Well I'll mention it to pyright and maybe take a look at mypy eventually if it can be small contribution there. |
In general, trying to type various aspects of the runtime implementation of typing has been notoriously fragile with little benefit.
So yeah, nice if you could make it work for cheap, but seems like that's not the case :-) Maybe we should put a comment warning in the file. |
Resolves #7811
I'm unsure why Generic is a _SpecialForm when at runtime it's normal class. I tried to make smallest change to Generic that just adds
__orig_bases__
.