(🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza revisando el uso propuesto de StubOnlyBase y las discusiones enlazadas; después, inspecciona los stubs mencionados _collections_abc.pyi, typing.pyi y builtins.pyi. Determina la semántica prevista del decorador y su comportamiento según la versión antes de proponer cómo se abordarían los ejemplos y los issues relacionados; se considerará completado cuando haya acuerdo sobre el diseño y su efecto en los stubs afectados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100