summaryrefslogtreecommitdiffstats
path: root/Doc/library/types.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/types.rst')
-rw-r--r--Doc/library/types.rst40
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