Skip to content

Preserve deferred annotations in FunctionMaker and decoratorx - #194

Open
mayankdhingra02 wants to merge 1 commit into
micheles:masterfrom
mayankdhingra02:fix-deferred-wrapper-annotations
Open

mayankdhingra02 wants to merge 1 commit into
micheles:masterfrom
mayankdhingra02:fix-deferred-wrapper-annotations

Conversation

@mayankdhingra02

Copy link
Copy Markdown

decoratorx still reads func.__annotations__ while generating a wrapper,
which raises NameError when deferred annotations reference names imported
only under TYPE_CHECKING.

When that happens, keep the original __annotate__ callback on the generated
function. This lets decoration and calls succeed while allowing annotations
to resolve later against the original globals and captured locals. Concrete
and future-string annotations retain their existing dictionary identity.

Fixes #177.

Verification:

  • Added regressions for deferred generic annotations, later definitions,
    stacked wrappers, captured local classes and async wrappers, plus controls
    for concrete and string annotations. Four cases fail before the fix.
  • Full PYTHONPATH=src python tests/test.py -v passes on Python 3.8 through
    3.15, including all documentation doctests. Deferred-only tests skip on
    versions before 3.14.
  • The CI codespell and flake8 commands, python -m mypy.stubtest decorator
    and python -m mypy src/decorator/__init__.py all pass.

Keep unresolved annotations from preventing FunctionMaker from generating a
wrapper. Forward the original annotation callback so its names can resolve
later in the original globals and closure, including through decoratorx.

Keep the existing annotation mapping for concrete and string annotations.
Cover deferred generic annotations, later definitions, stacked and async
wrappers, and annotations that capture local classes.

Fixes micheles#177.

Signed-off-by: Mayank Dhingra <mayankdhingra02@gmail.com>
@micheles

Copy link
Copy Markdown
Owner

Thank you for your contribution, it is a good idea but I would make some minor change. It will be back on this in a few days, now I am travelling.

This branch has not been deployed

No deployments
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.

decoratorx requires from __future__ import annotations although it shouldn't in Python 3.15 (was: 3.14)

2 participants