`get_original_bases` does not return what `cls.__orig_bases__` returns
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
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
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece em Lib/types.py, em get_original_bases, e reproduza o exemplo de herança da issue, incluindo o caso da classe Three. Está concluído quando a função corresponder ao comportamento documentado de original-bases e a cobertura de regressão verificar o caso do atributo herdado; revise o PR vinculado gh-156917 antes de começar.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- tooling
- Tipo de issue
- Bug
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 25/100