(🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are not Generics, they define __class_getitem__) and some are still not subscribe-able but state they are in the stubs, for instance _collections_abc.dict_keys or any of the collections pre 3.9.
It could be used something like:
# _collections_abc.pyi
@StubOnlyBase(KeysView[_KT_co], Generic[_KT_co, _VT_co])
class dict_keys:
...
# typing.pyi
@StubOnlyBase(Generic[_T])
class Container:
if sys.version_info >= (3, 9):
def __class_getitem__(???) -> ???:
@StubOnlyBase(Collection[_KT], Generic[_KT, _VT_co])
class Mapping(Collection):
...
Mapping doesn't actually extend Generic, but Container defines __class_getitem__, so that would also need to be updated in the stubs.
This might help resolve https://github.com/python/mypy/issues/3186, but still indicate that it's not actually one of the bases.
# builtins.pyi
@StubOnlyBase(numbers.Integral)
class int:
...
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(https://github.com/python/mypy/issues/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:
from typing import _alias # type: ignore[attr-defined]
from typing import TYPE_CHECKING
from _collections_abc import dict_keys
if not TYPE_CHECKING:
dict_keys = _alias(dict_keys, 2)
def foo(dk: dict_keys[str, int]) -> None: ...
https://github.com/python/typeshed/pull/6312#issuecomment-976036379
https://github.com/python/typeshed/issues/6257
Obligatory shill to #953, which I think would render this moot.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposed StubOnlyBase usage and the linked discussions, then inspect the named _collections_abc.pyi, typing.pyi, and builtins.pyi stubs. Determine the decorator's intended semantics and version behavior before proposing how the examples and related issues would be addressed; done requires agreement on the design and its effect on the affected stubs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100