Skip to content

Upgrade Lib/types.py from Python 3.13.5 #5928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions Lib/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Define names for built-in types that aren't directly accessible as a builtin.
"""

import sys

# Iterators in Python aren't a matter of type but of protocol. A large
Expand Down Expand Up @@ -52,17 +53,14 @@ def _m(self): pass

try:
raise TypeError
except TypeError:
tb = sys.exc_info()[2]
TracebackType = type(tb)
FrameType = type(tb.tb_frame)
tb = None; del tb
except TypeError as exc:
TracebackType = type(exc.__traceback__)
FrameType = type(exc.__traceback__.tb_frame)

# For Jython, the following two types are identical
GetSetDescriptorType = type(FunctionType.__code__)
MemberDescriptorType = type(FunctionType.__globals__)

del sys, _f, _g, _C, _c, _ag # Not for export
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export


# Provide a PEP 3115 compliant mechanism for class creation
Expand All @@ -82,7 +80,7 @@ def resolve_bases(bases):
updated = False
shift = 0
for i, base in enumerate(bases):
if isinstance(base, type) and not isinstance(base, GenericAlias):
if isinstance(base, type):
continue
if not hasattr(base, "__mro_entries__"):
continue
Expand Down Expand Up @@ -146,6 +144,35 @@ 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.__dict__.get("__orig_bases__", 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__.

Expand All @@ -158,7 +185,7 @@ class DynamicClassAttribute:
attributes on the class with the same name. (Enum used this between Python
versions 3.4 - 3.9 .)

Subclass from this to use a different method of accessing virtual atributes
Subclass from this to use a different method of accessing virtual attributes
and still be treated properly by the inspect module. (Enum uses this since
Python 3.10 .)

Expand Down Expand Up @@ -305,4 +332,11 @@ def wrapped(*args, **kwargs):
NoneType = type(None)
NotImplementedType = type(NotImplemented)

def __getattr__(name):
if name == 'CapsuleType':
import _socket
return type(_socket.CAPI)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

__all__ = [n for n in globals() if n[:1] != '_']
__all__ += ['CapsuleType']
Loading