Skip to content

Change names of builtin types exposed in the types module #100129

Open
@serhiy-storchaka

Description

@serhiy-storchaka

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 failed

I 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions