`get_original_bases` does not return what `cls.__orig_bases__` returns
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug report
Bug description:
The types documentation states for the types.get_original_bases function:
For classes that have an
__orig_bases__attribute, this function returns the value ofcls.__orig_bases__. For classes without the__orig_bases__attribute,cls.__bases__is returned.
I need the functionality of cls.__orig_bases__, but need to use types.get_original_bases to make the type checker happy.
A short MRE:
from types import get_original_bases
from typing import Generic, TypeVar
T = TypeVar("T")
class One(Generic[T]):
pass
class Two(One[int]):
pass
class Three(Two):
pass
assert get_original_bases(One) == One.__orig_bases__
assert get_original_bases(Two) == Two.__orig_bases__
assert get_original_bases(Three) == Three.__orig_bases__
# Traceback (most recent call last):
# File "c:\<cut>\test.py", line 34, in <module>
# assert get_original_bases(Three) == Three.__orig_bases__
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# AssertionError
This is a easy solve, I would be happy to add a PR. Change here:
try:
- return cls.__dict__.get("__orig_bases__", cls.__bases__)
+ return getattr(cls, "__orig_bases__", cls.__bases__)
except AttributeError:
raise TypeError(
f"Expected an instance of type, not {type(cls).__name__!r}"
) from None
To show this works:
from typing import Generic, TypeVar
T = TypeVar("T")
class One(Generic[T]):
pass
class Two(One[int]):
pass
class Three(Two):
pass
def better_get_original_bases(cls):
try:
return getattr(cls, "__orig_bases__", cls.__bases__)
except AttributeError:
raise TypeError(
f"Expected an instance of type, not {type(cls).__name__!r}"
) from None
assert better_get_original_bases(One) == One.__orig_bases__
assert better_get_original_bases(Two) == Two.__orig_bases__
assert better_get_original_bases(Three) == Three.__orig_bases__
# <No output>
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
- gh-156917
Guía de contribución
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
Empieza en Lib/types.py, en get_original_bases, y reproduce el ejemplo de herencia del issue, incluido el caso de la clase Three. Se considera terminado cuando la función coincide con el comportamiento documentado de original-bases y la cobertura de regresión verifica el caso del atributo heredado; revisa el PR vinculado gh-156917 antes de empezar.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 25/100