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
# Redirect method attribute access to the underlying function. The C code
# verifies that the __func__ attribute is some kind of callable, so we need
# to look for attributes recursively.
if isinstance(annotate, types.MethodType):
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, but I don't think we should add all this complexity to support increasingly exotic kinds of callables. Things that quack like a function (in terms of attributes) should work and we should give reasonable errors if something goes wrong, but I don't think we should go out of our way to support various other stdlib callables and try to figure out what the user meant.

Copy link
Contributor Author

@dr-carlos dr-carlos Nov 20, 2025

Choose a reason for hiding this comment

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

Sure!

No other callables have the exact same attribute layout as functions (hence why this PR exists), but I'm happy to reduce the scope and thus complexity.
Currently this supports:

  • Methods of any callable
  • Class and generic objects, with any callable as their __init__ method
  • Callable instances, with any callable as their __call__ method
  • functools.partial and functools.partialmethod, wrapping any callable
  • Wrapped functions/callables

What range of support do you think would be reasonable?

  • Normal class methods seem just like functions to the user (albeit bound), support for them would make sense. Plus, this would presumably provide most of the benefit to be gained from supporting non-function annotates.
  • Using classes themselves as an annotate function is pretty esoteric, happy to remove this, especially as supporting them introduces the most complexity.
  • Most other callables in the stdlib (and I'd bet outside of as well) are simply implemented as Python classes overriding __call__, so it makes sense to me to support these.
  • functools.partial (but to a lesser extent functools.partialmethod) probably 'feel' closest to real functions (to someone unfamiliar with the internals, at least), so I'd be inclined to support these. Happy to only support wrapping normal functions and not arbitrary callables though.
  • Wrapped functions are already unwrapped in other parts of annotationlib and are pretty easy to support, but if you'd rather not automatically unwrap anything here, I'm happy to remove it.

Anyway, those are my thoughts - please let me know which parts you'd like me to remove/simplify :)

Copy link
Member

Choose a reason for hiding this comment

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

I don't think any of them should be supported (except to the extent they work because they look like functions).

@dr-carlos dr-carlos closed this Dec 5, 2025
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.

2 participants