Skip to content

PEP 649: class __annotate__ is shadowed by compiler-generated one #133037

Description

@luigig44

Bug report

Bug description:

I'm playing around with PEP 649, PEP 749 annotations and I found this surprising behaviour:

class C:
    x: str
    def __annotate__(format):
        if format != 1:
            raise NotImplementedError()
        return {'x': int}

assert C.__annotate__(1) == {'x': int} # This returns {'x': str} and fails

I'm not yet sure why I would write code with annotations and then also write an explicit __annotate__, but I would expect the explicit method to prevail over the compiler-generated one. If I explicitly set C.__annotate__ = some_func afterwards it works as I would expect.

Calling dis on the provided example shows both __annotate__s getting compiled, and one overwriting the other in the class.

Related to #119180.

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

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

    3.14bugs 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