gh-141388: Improve support for non-function callables as annotate functions #142327
+255
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
call_annotate_function()tries to run with fake globals but can't find the annotate function's__code__attribute.__defaults__, that's fine, just assume it'sNone.If required, I can remove some (or all) of these defaults, particularly
__globals__which adds an extra parameter to_build_closure(), or__builtins__which is semantically correct but a little more complicated than the rest (it would be much simpler to just fallback tobuiltins.__dict__).annotationlibdocumentation providing a simple recipe for a 'minimum viable implementation' of a custom callable class as annotate function. Happy to remove if this is unnecessary/too niche - I just thought it was the clearest way of demonstrating how to implement one of these, without just spelling out which dunders they need to implement.__annotate__Functions don't actually need to be functions #141388📚 Documentation preview 📚: https://cpython-previews--142327.org.readthedocs.build/