Repository navigation
(🎁) Add a StubOnlyBase decorator for types that don't actually extend things, but do in the stubs (like Generic) #956
Description
Activity
- addedtopic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
on Nov 23, 2021 - changed the title
[-]Add a `StubOnlyGeneric` annotation for generic types that don't extend `Generic`[/-][+]Add a `StubOnlyGeneric` decorator for generic types that don't extend `Generic`[/+]on Nov 23, 2021 - changed the title
[-]Add a `StubOnlyGeneric` decorator for generic types that don't extend `Generic`[/-][+]Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)[/+]on Nov 23, 2021 I like this idea! It would reduce the need for PRs like python/cpython#29355, which corrects an annoying inconsistency between the stub file and the runtime code, but is unlikely to get merged (no real use case, if we're honest).
Isn't the use case making the stubs more accurate mainly around type parameters?
That's the use case for making
singledispatchmethodgeneric in the stubs, of course. But I, for one, have no real use case for being able to parameterizesingledispatchmethodat runtime, other than wanting consistency with the stubs. Can you think of a situation when you'd want to usesingledispatchmethodin a type annotation, and therefore need to parameterize it?As per the OP, my use case is mainly around
dict_keysand the rest of thecollectionspre 1.9, also registeredabcsI think we might be talking at cross purposes 🙂
As I said in my first message in this thread, I agree with your case — I like your idea! I was saying that there wasn't much of a use case for my previous PR to cpython that I linked to here, and that I liked your idea because it would reduce the need for PRs to cpython such as the one I linked to. My "no real use case, if we're honest" comment was referring to my PR to cpython, not your idea here 🙂
Reacted by KotlinIslandI think python/typeshed#7436 would be a really nice use case for this.
The existing
typingdecorators all use snake_case, so I think this new decorator should also use snake case (in accordance with PEP 8). I don't think stub-only should be in the name, because afaik everything currently intypingcan also be used in regular.pyfiles, so I think we should keep it that way.There already is
@typing.type_check_only, so@typing.type_check_only_base_classeswould be analogous ... it is however a bit too verbose for my liking. I think simply@typing.base_classesshould be fine, what do you think?I guess getting this into Python would require a PEP?
CC: @srittau, @JelleZijlstra
- changed the title
[-]Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)[/-][+](🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)[/+]on Jul 3, 2022
There are existing classes that extend things in an abstract way, like ABCs, and things that are
Genericin theory but not in reality, for example all thecollections(the collection classes are notGenerics, they define__class_getitem__) and some are still not subscribe-able but state they are in the stubs, for instance_collections_abc.dict_keysor any of the collections pre 3.9.It could be used something like:
Mappingdoesn't actually extendGeneric, butContainerdefines__class_getitem__, so that would also need to be updated in the stubs.This might help resolve python/mypy#3186, but still indicate that it's not actually one of the bases.
I think a change like this would make the stubs much more understandable and closer to the actual implementation, also fixing issues about older python versions not working properly with the current stubs(python/mypy#11529).
I personally find it very confusing when referencing the stubs that the bases are often false.
No idea how this would be used in a real life annotation, but a workaround that works is:
python/typeshed#6312 (comment)
python/typeshed#6257
Obligatory shill to #953, which I think would render this moot.