Expose combined __annotations__ on composed functions (#496) - #634
Open
az51826295-sys wants to merge 1 commit into
Open
Expose combined __annotations__ on composed functions (#496)#634az51826295-sys wants to merge 1 commit into
__annotations__ on composed functions (#496)#634az51826295-sys wants to merge 1 commit into
Conversation
Compose already synthesizes __signature__ from the first-applied function's parameters and the last-applied function's return annotation, but it had no __annotations__, so typing.get_type_hints() and tools that read __annotations__ directly saw nothing on a composed callable. Add an instance property that builds the same view: parameter annotations from the first function applied, the return annotation from the last. Functions without annotations contribute nothing, as do callables that have no __annotations__ at all. This is a small runtime step toward the static typing discussed in pytoolz#496 (the composed callable now reports Callable[[A], C] for compose(g, f) with f: A -> B, g: B -> C); it does not add stubs or annotate the library itself. Adds test_compose_annotations covering the combined view, unannotated pieces, get_type_hints, and consistency with __signature__. Co-Authored-By: Rookery Alpha (AI agent, human-reviewed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Composealready synthesizes__signature__(parameters of the first function applied, return annotation of the last), but it had no__annotations__, sotyping.get_type_hints()and tools that read__annotations__directly saw nothing on a composed callable.This adds an instance property that builds the same view:
Functions without annotations contribute nothing; callables with no
__annotations__at all (e.g.str, arbitrary objects) are handled.Scope: this is a small runtime step toward the static typing discussed in #496 — the composed callable now reports
Callable[[A], C]forcompose(g, f)withf: A -> B,g: B -> C. It does not add stubs or annotate the library itself; happy to split or drop it if you'd rather approach #496 differently.Tests:
test_compose_annotations(combined view, unannotated pieces,get_type_hints, consistency with__signature__). Full suite incl. doctests passes locally (pytest --doctest-modules toolz/; the only failure istest_has_version, which needs an installed distribution and fails identically on master in my environment).Authored with the help of an AI agent (Rookery Alpha), human-reviewed.