Change names of builtin types exposed in the types module

serhiy-storchaka · GitHub

Some builtin types (like int, range, map) are exposed in the builtins module under their names. int.__name__ is "int" and int.__module__ is "builtins". getattr(sys.modules[int.__module__], int.__name__) is int

Other builtin types are exposed in the types module, but their __module__ attribute is still "builtins", and their __name__ attribute is different from the name under which they are accessible in the types module. The relation between names is not obvious, when you see <class 'builtin_function_or_method'>, it is hard to defer that it is types.BuiltinFunctionType.

As result, these types cannot be pickled:

>>> pickle.dumps(types.ClassMethodDescriptorType) Traceback (most recent call last): File "<stdin>", line 1, in <module> _pickle.PicklingError: Can't pickle <class 'classmethod_descriptor'>: attribute lookup classmethod_descriptor on builtins failedI propose to change attributes __module__, __name__ and __qualname__ of all builtin types exposed in the types modules so that they will match names under which they are accessible.

Linked PRs

gh-100129: Make the names of all classes in the types module resolvable #100130

gh-100129: Add tests for pickling all builtin types and functions #100142

[3.11] gh-100129: Add tests for pickling all builtin types and functions (GH-100142) #100405

[3.10] gh-100129: Add tests for pickling all builtin types and functions (GH-100142) #100406