Skip to content

Make __signature__ a lazily computed property#451

Merged
eb8680 merged 7 commits intomasterfrom
jf-lazy-signature
Mar 10, 2026
Merged

Make __signature__ a lazily computed property#451
eb8680 merged 7 commits intomasterfrom
jf-lazy-signature

Conversation

@jfeser
Copy link
Copy Markdown
Contributor

@jfeser jfeser commented Dec 19, 2025

A first attempt to address #450. Handles the basic case, but forward references have some caveats. typing.get_type_hints doesn't actually seem to use the lexical scope of the definition to resolve the hints. Instead, it looks for the module containing the definition. This means that forward references don't work when (for example) an operation is defined inside a function.

@jfeser
Copy link
Copy Markdown
Contributor Author

jfeser commented Dec 19, 2025

Separately, I think making __signature__ derive from __default__ makes the Operation interface somewhat simpler.

@jfeser jfeser requested a review from eb8680 December 19, 2025 17:43
@eb8680
Copy link
Copy Markdown
Contributor

eb8680 commented Dec 19, 2025

IIUC we're going to end up reimplementing a lot of the new Python 3.14 standard library module annotationlib this way. In particular annotationlib includes new and improved versions of inspect.get_annotations() and typing.ForwardRef and a new typing function typing.evaluate_forward_ref, all of which are also backported to 3.12/13 within typing_extensions.

From the annotationlib documentation it seems to be the case that using evaluate_forward_ref on the output of get_annotations will evaluate hints containing ForwardRefs in the correct scopes, and that this is now recommended as the primary method for evaluating and introspecting annotations with forward references.

Copy link
Copy Markdown
Contributor

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

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

This is now needed to fix a bug downstream

@eb8680 eb8680 linked an issue Mar 9, 2026 that may be closed by this pull request
@eb8680 eb8680 merged commit fa2e420 into master Mar 10, 2026
6 checks passed
@eb8680 eb8680 deleted the jf-lazy-signature branch March 10, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

defdata fails on operations with forward references in their return type

2 participants