Sitelet https://github.com/python/cpython/issues/100129
Skip to content

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

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

Activity

  1. added a commit that references this issue on Dec 9, 2022
  2. vstinner commented on Dec 9, 2022

    @vstinner
    Member

    It sounds like a good idea.

    Changing names like builtin_function_or_method will impact doctests, but I think that it's worth it.

  3. AlexWaygood commented on Dec 9, 2022

    @AlexWaygood
    Member

    +1 to this idea. This will make life easier for tools that do dynamic inspection of Python at runtime. The current situation makes life difficult at times for, e.g., mypy's stubtest tool.

  4. serhiy-storchaka commented on Dec 9, 2022

    @serhiy-storchaka
    MemberAuthor

    I proposed this idea several years ago, and perhaps even an issue was created (but I cannot find it now). Guido was positive about this, but I had some doubts. The original code was written in 2018, now I'm going to finish it.

  5. markshannon commented on Dec 9, 2022

    @markshannon
    Member

    Changing the names of common types like NoneType and generator is going to break a lot of stuff.
    The number of test changes required for the PR suggests that this would cause widespread breakage.

    There must be other ways to make these objects pickleable.

  6. added a commit that references this issue on Dec 9, 2022
  7. serhiy-storchaka commented on Dec 21, 2022

    @serhiy-storchaka
    MemberAuthor

    Yes, it will break some doctests. The main purpose of this change is not making these types pickleable (a special case was already added for NoneType, EllipsisType and NotImplementedType, and it can be extended for other types), but making them more uniform with other types.

    The breakage of doctests can be reduced if we omit the module name in repr and error messages if __module__ == "types". Currently it is omitted for "builtins", and, in some cases, for "__main__", we can add a special case for "types". It is not a simple change, because it will require to make changes in several places. What do you think about this?

  8. added a commit that references this issue on Dec 21, 2022
  9. added 4 commits that reference this issue on Dec 21, 2022
  10. jamesbraza commented on Jun 28, 2023

    @jamesbraza

    Hello all, just chiming in on this. I think the core of this issue is:

    1. Standardize types, and break users who depended on prior naming special cases
      • Note: naming special cases don't 1-1 correspond with Python docs
    2. Leave types unstandardized, and require all new users to implement special cases

    Just to share my "user story":

    • I got bit by types being nonstandard today, when doing deserialization based on __module__/__qualname__
    • I have been confused about types being shown as traceback, when the docs say it should be types.TracebackType

    I think generally removing unexpected confusion for future users is a worthwhile benefit of #100130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions