Fix FunctionMaker.create() with return type annotation (#138) - #186
Merged
Merged
Conversation
FunctionMaker.create() raised a SyntaxError when the signature string
contained a return annotation, e.g. create('add(a: int) -> int', ...).
The parser assumed the signature always ended with ')' and stripped the
last character, then unconditionally re-appended ')' in the def line,
producing invalid source such as 'def add(a: int) -> in):'.
Split the argument list from an optional '-> ...' return annotation using
rpartition(')'), and reattach the annotation after the closing paren when
emitting the def line. Signatures without an annotation are unchanged.
Fixes micheles#138.
Owner
|
Thanks for your contribution, you must understand that the decorator module is old and predates annotations |
micheles
approved these changes
Jul 9, 2026
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.
Summary
FunctionMaker.create()raised aSyntaxErrorwhen the signature string contained a return annotation, e.g.:The parser assumed the signature always ended with
)and stripped the last character withrest[:-1], then unconditionally re-appended)when emitting thedefline. With a return annotation present, this produced invalid source such as:which failed to compile. This matches the report in #138 (and the earlier acknowledgement in #114).
Fix
In
FunctionMaker.create(), split the argument list from an optional-> ...return annotation usingrest.rpartition(')'), and reattach the annotation after the closing paren when building thedefline (via a%(return_annotation)splaceholder so it is substituted safely). Signatures without a return annotation are handled exactly as before (rpartitionyields an empty tail), so behavior is unchanged for the existing case.Tests / Verification
ExtraTestCase.test_return_annotationcovering both a signature without an annotation and one with a-> intreturn annotation. It fails onmasterwithSyntaxError: unmatched ')'and passes with this change.python tests/test.py -v->Ran 26 tests ... OK(including the doctest suite).flake8(CI config),codespell(CI config),python -m mypy.stubtest decorator, andpython -m mypy src/decorator/__init__.pyall pass.CHANGES.mdentry under "Unreleased".Disclosure: prepared with AI assistance; reviewed and verified locally.