Skip to content

__annotate__ functions generated by CPython for class methods have an incorrect __qualname__ #137814

Description

@DavidCEllis

Bug report

Bug description:

The __qualname__ of __annotate__ functions of methods of classes incorrectly refers to the __annotate__ function of the class itself.

class Example:
    x: int
    def func(self, x: int) -> str: ...

print(f"{Example.__annotate__.__qualname__ = }")
print(f"{Example.func.__annotate__.__qualname__ = }")

Output:

Example.__annotate__.__qualname__ = 'Example.__annotate__'
Example.func.__annotate__.__qualname__ = 'Example.__annotate__'

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. ZeroIntensity commented on Aug 15, 2025

    @ZeroIntensity
    Member
  2. added
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Aug 15, 2025
  3. JelleZijlstra commented on Aug 16, 2025

    @JelleZijlstra
    Member

    This is also similar for __annotate__ functions on global functions:

    >>> def f() -> None: pass
    ... 
    >>> f.__annotate__.__qualname__
    '__annotate__'
    

    Note arguably fixing this requires a magic number bump since it changes compiler output. Is this worth the disruption of a magic number bump? I'm not sure.

  4. added a commit that references this issue on Aug 16, 2025
  5. serhiy-storchaka commented on Dec 23, 2025

    @serhiy-storchaka
    Member

    See #143067 -- it breaks pickling.

  6. glemaitre commented on Apr 7, 2026

    @glemaitre

    Is this worth the disruption of a magic number bump? I'm not sure.

    Just to provide a end-user feedback to give some particular context to the previous comment: on the scientific python side, we might have some parallel processing that would try to pickle some object coming from a class with annotations.

    Not back-porting to 3.14 would mean that an entire library will not be compatible with a version of Python which looks strange I think.

  7. JelleZijlstra commented on Apr 7, 2026

    @JelleZijlstra
    Member

    Perhaps you can work around this by dropping the annotations before pickling? Do you really need the annotations in the parallel workers?

  8. glemaitre commented on Apr 7, 2026

    @glemaitre

    It looks something interesting to explore. I'll definitely have a look. Thanks @JelleZijlstra

  9. added a commit that references this issue on Apr 7, 2026
  10. JelleZijlstra commented on Apr 7, 2026

    @JelleZijlstra
    Member

    I found an alternative fix that doesn't require bumping the magic number, so let's do that for 3.14: #148221.

    #137842 is still the better long-term solution so I plan to land that for 3.15+.

  11. added 2 commits that reference this issue on Apr 16, 2026
  12. JelleZijlstra commented on Apr 16, 2026

    @JelleZijlstra
    Member

    This is now fixed on both 3.14 and main, though with different solutions.

  13. glemaitre commented on Apr 16, 2026

    @glemaitre

    Thanks a bunch.

  14. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixes3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions