Skip to content

Fix nested class annotations in import-hooked methods - #595

Open
HanpuLi wants to merge 2 commits into
agronholm:masterfrom
HanpuLi:fix/enum-nested-default-annotation
Open

HanpuLi wants to merge 2 commits into
agronholm:masterfrom
HanpuLi:fix/enum-nested-default-annotation

Conversation

@HanpuLi

@HanpuLi HanpuLi commented Sep 19, 2026 •

Copy link
Copy Markdown

Changes

Fixes #402.

The import hook injects annotation checks into method bodies, but method bodies do not inherit the containing class namespace. An annotation such as value: NestedEnum is valid while the class body is being built, then becomes an unresolved bare name when the instrumented method is called.

Track direct nested class names while transforming the class body and resolve those names through the implicit __class__ closure in method annotations. The class body is pre-scanned, so explicit/forward annotations also work when the referenced nested class is declared later in the class body. Module/global names remain unchanged.

The regression coverage includes the nested Enum case from #402, an invalid value that must still raise TypeCheckError, and a forward reference to a nested enum declared later in the class body. The existing separate @typechecked limitation remains xfailed and documented.

Local verification:

  • coverage run -m pytest: 543 passed, 5 skipped, 6 xfailed
  • pre-commit run --all-files: passed (including the project's mypy hook)
  • sphinx-build -W -n docs <tmpdir>: passed
  • Ruff check and format check on changed files
  • git diff --check

Checklist

  • Changes have tests
  • Documentation/changelog updated

@coveralls

coveralls commented Sep 19, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 94.867% (+0.01%) from 94.853% — HanpuLi:fix/enum-nested-default-annotation into agronholm:master

@HanpuLi
HanpuLi force-pushed the fix/enum-nested-default-annotation branch from b19c612 to d75c01f Compare September 19, 2026 13:44
@HanpuLi HanpuLi changed the title Fix class-local annotations in import-hooked methods Fix nested class annotations in import-hooked methods Sep 19, 2026
@HanpuLi
HanpuLi force-pushed the fix/enum-nested-default-annotation branch from f7c7770 to f1702dc Compare September 20, 2026 16:50

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.

NameError on Enum

2 participants