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 /Doc/library | |
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 'Doc/library')
-rw-r--r-- | Doc/library/types.rst | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 27b9846..54887f4 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -82,6 +82,46 @@ Dynamic Type Creation .. versionadded:: 3.7 +.. function:: get_original_bases(cls, /) + + Return the tuple of objects originally given as the bases of *cls* before + the :meth:`~object.__mro_entries__` method has been called on any bases + (following the mechanisms laid out in :pep:`560`). This is useful for + introspecting :ref:`Generics <user-defined-generics>`. + + For classes that have an ``__orig_bases__`` attribute, this + function returns the value of ``cls.__orig_bases__``. + For classes without the ``__orig_bases__`` attribute, ``cls.__bases__`` is + returned. + + 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 Bar.__bases__ == (Foo, float) + assert get_original_bases(Bar) == (Foo[int], float) + + assert Baz.__bases__ == (list,) + assert get_original_bases(Baz) == (list[str],) + + assert Eggs.__bases__ == (tuple,) + assert get_original_bases(Eggs) == (NamedTuple,) + + assert Spam.__bases__ == (dict,) + assert get_original_bases(Spam) == (TypedDict,) + + assert int.__bases__ == (object,) + assert get_original_bases(int) == (object,) + + .. versionadded:: 3.12 + .. seealso:: :pep:`560` - Core support for typing module and generic types |