`get_original_bases` does not return what `cls.__orig_bases__` returns
オープン
まだ誰も着手していません。
docs
topic-typing
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Lib/types.py の get_original_bases から始め、Three クラスの場合を含めて、issue の継承例を再現してください。関数が文書化された original-bases の動作に一致し、回帰テストのカバレッジで継承された属性のケースが検証されれば完了です。開始前にリンクされた PR gh-156917 を確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100