GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Change names of builtin types exposed in the types module · Issue #100129 · python/cpython

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

New issue
Copy link
New issue
Copy link
Open
Open
Change names of builtin types exposed in the types module#100129
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

Description

@serhiy-storchaka
serhiy-storchaka
opened on Dec 9, 2022
Issue body actions

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

  • 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
Reacted by Alex Waygood, James Braza and Rafal Krupinski

Activity

  1. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Dec 9, 2022
  2. serhiy-storchaka
    added this to Pickle and copy issues 🥒on Dec 9, 2022
  3. serhiy-storchaka
    added a commit that references this issue on Dec 9, 2022

    pythongh-100129: Make the names of all classes in the types module re…

    4f908e8
  4. vstinner commented on Dec 9, 2022

    @vstinner
    vstinner
    on Dec 9, 2022
    Member
    More actions

    It sounds like a good idea.

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

  5. AlexWaygood commented on Dec 9, 2022

    @AlexWaygood
    AlexWaygood
    on Dec 9, 2022
    Member
    More actions

    +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.

  6. serhiy-storchaka commented on Dec 9, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Dec 9, 2022
    MemberAuthor
    More actions

    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.

    Reacted by Alex Waygood
  7. markshannon commented on Dec 9, 2022

    @markshannon
    markshannon
    on Dec 9, 2022
    Member
    More actions

    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.

  8. serhiy-storchaka
    added a commit that references this issue on Dec 9, 2022

    pythongh-100129: Add tests for pickling all builtin types and functions

    e8254bb
  9. serhiy-storchaka commented on Dec 21, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Dec 21, 2022
    MemberAuthor
    More actions

    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?

  10. serhiy-storchaka
    added a commit that references this issue on Dec 21, 2022

    gh-100129: Add tests for pickling all builtin types and functions (GH…

    b98d2d3
  11. miss-islington
    added 4 commits that reference this issue on Dec 21, 2022

    pythongh-100129: Add tests for pickling all builtin types and functio…

    bf3c085

    pythongh-100129: Add tests for pickling all builtin types and functio…

    c3e826e

    gh-100129: Add tests for pickling all builtin types and functions (GH…

    2dda668

    gh-100129: Add tests for pickling all builtin types and functions (GH…

    0397f04
  12. iritkatriel
    added a commit that references this issue on Dec 28, 2022

    Merged main into regmachine branch (#50)

    fdc73cb
  13. jamesbraza commented on Jun 28, 2023

    @jamesbraza
    jamesbraza
    on Jun 28, 2023
    More actions

    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

  14. serhiy-storchaka
    marked types.FunctionType is not included in builtin library #92316 as a duplicate of this issue on Jun 2, 2026
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)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Pickle and copy issues 🥒
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.