diff options
author | James Hilton-Balfe <gobot1234yt@gmail.com> | 2023-04-23 19:24:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-23 19:24:30 (GMT) |
commit | 730bbddfdf610343a2e132b0312d12254c3c73d6 (patch) | |
tree | 2c4b223f290866cc428d3faa0a7cf97c75128cb8 /Lib/types.py | |
parent | 05b3ce7339b9ce44eec728e88e80ba1f125436ed (diff) | |
download | cpython-730bbddfdf610343a2e132b0312d12254c3c73d6.zip cpython-730bbddfdf610343a2e132b0312d12254c3c73d6.tar.gz cpython-730bbddfdf610343a2e132b0312d12254c3c73d6.tar.bz2 |
gh-101688: Implement types.get_original_bases (#101827)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Lib/types.py')
-rw-r--r-- | Lib/types.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/types.py b/Lib/types.py index aa8a1c8..6110e6e 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -143,6 +143,38 @@ def _calculate_meta(meta, bases): "of the metaclasses of all its bases") return winner + +def get_original_bases(cls, /): + """Return the class's "original" bases prior to modification by `__mro_entries__`. + + Examples:: + + from typing import TypeVar, Generic, NamedTuple, TypedDict + + T = TypeVar("T") + class Foo(Generic[T]): ... + class Bar(Foo[int], float): ... + class Baz(list[str]): ... + Eggs = NamedTuple("Eggs", [("a", int), ("b", str)]) + Spam = TypedDict("Spam", {"a": int, "b": str}) + + assert get_original_bases(Bar) == (Foo[int], float) + assert get_original_bases(Baz) == (list[str],) + assert get_original_bases(Eggs) == (NamedTuple,) + assert get_original_bases(Spam) == (TypedDict,) + assert get_original_bases(int) == (object,) + """ + try: + return cls.__orig_bases__ + except AttributeError: + try: + return cls.__bases__ + except AttributeError: + raise TypeError( + f'Expected an instance of type, not {type(cls).__name__!r}' + ) from None + + class DynamicClassAttribute: """Route attribute access on a class to __getattr__. |