-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
__annotate__ Functions don't actually need to be functions #141388
Copy link
Copy link
Open
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Fields
Give feedbackNo fields configured for issues without a type.
Bug report
Bug description:
According to the Python docs, annotate functions need to be actual functions. However, the C code doesn't enforce this, it enforces (using
PyCallable_Check()) that the annotate 'function' is any callable:cpython/Objects/funcobject.c
Lines 865 to 888 in 9cb8c52
This initially seems fine, but leads to some weird bugs.
And similar for
get_annotations(f, format=Format.FORWARDREF), trying to find the annotation function's__builtins__, which only exists on functions (not arbitrary callables).I'm happy to make a PR to replace the
PyCallable_Check()withPyFunction_Check()in the relevant C code for setting__annotate__, but I guess this might be a breaking change? I'm not sure, so I thought I'd check first before making it. The other option would be checking for the presence of__closure__/__builtins__/etc. before accessing them inannotationlib.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs