Skip to content

Conversation

@dr-carlos
Copy link
Contributor

@dr-carlos dr-carlos commented Nov 12, 2025

  • Any callable can be used as an __annotate__ function in 3.14, but annotationlib will start throwing errors if the format is not implemented.
  • This is an initial implementation to support them as first-class citizens included here, mostly in call_annotate_function()
  • Currently this implementation supports: methods, class/static methods, class instances that are callable, classes and generics themselves, partial, wrapped, singledispatch, and cached functions. 'Builtin' (C) callables can't really be supported because they have no __code__, __globals__, etc., so a ForwardRef or stringification using the current techniques is impossible. Let me know if there are any more callables to implement, some more special-casing will probably be required.
  • Initial tests are present but not thorough. It should work for all parts of annotationlib, but tests are mostly present on call_annotate_function() for now.
  • Opening a PR now to seek feedback on the implementation and decide on whether the extra complexity is worth it. If there is interest, I'll add more thorough tests, etc.

Copy link
Contributor Author

@dr-carlos dr-carlos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current status: overall, there is a decent amount of special-casing going on to fully support various callable types. But at this point, it supports most stdlib callables (at least, the ones, I could think of), except for C functions which don't have __code__ or __globals__ so can never work with Forwardref/_Stringifier. Some better error messages would be good for those types. More testing is definitely necessary to ensure full support.

if call_func := getattr(annotate.__call__, "__func__", None):
return getattr(call_func, attr, default)
elif isinstance(annotate, type):
return getattr(annotate.__init__, attr, default)
Copy link
Contributor Author

@dr-carlos dr-carlos Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that only the __init__ method is used for fake global namespaces when a class is used as an annotate function. Maybe the __new__ method should also count? Just adds a bit more complexity.


return default

def _direct_call_annotate(func, annotate, format):
Copy link
Contributor Author

@dr-carlos dr-carlos Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a poorly named function. Need some way tof describing that we're calling the full annotate function (annotate) based on the part of it that we compiled previously (func) with the given args (usually the format, unless we're recursively calling).

@dr-carlos dr-carlos changed the title gh-141388: Fully support callables as annotate functions gh-141388: Fully support non-function callables as annotate functions Nov 14, 2025
@dr-carlos
Copy link
Contributor Author

I've decided to take this PR out of draft mode as the tests are now pretty comprehensive and every stdlib callable I could think of is now supported where possible.
Open issues:

  • New internal function naming is possibly not great?
  • Classes as annotate functions currently only use the __init__ method for fake globals. The __new__ method is also called - should fake globals be inserted here too?
  • @functools.singledispatch functions don't work with fake globals because of the singledispatch.wrapper() function is stored in the __closure__ and annotationlib thinks this needs to be stringified. Is this an issue?
  • And I'll need to write up some documentation when I've got time.

@dr-carlos dr-carlos marked this pull request as ready for review November 18, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant