Skip to content

gh-160665: Recognize dataclass markers deferred by lazy imports - #160668

Open
LarryHu0217 wants to merge 1 commit into
python:mainfrom
LarryHu0217:codex/dataclass-lazy-initvar-160665
Open

LarryHu0217 wants to merge 1 commit into
python:mainfrom
LarryHu0217:codex/dataclass-lazy-initvar-160665

Conversation

@LarryHu0217

@LarryHu0217 LarryHu0217 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #160665.

When an unresolved lazy import causes annotation evaluation to fall back to a ForwardRef for the entire expression, annotations such as InitVar[A] are currently treated as ordinary dataclass fields. Reuse the existing annotation-prefix resolver for this representation.

This restores argument forwarding to __post_init__ and keeps affected ClassVar annotations out of instance fields. The original Field.type and ordinary deferred fields are preserved.

Adds deterministic circular-import fixtures covering direct, qualified and aliased InitVar, a ClassVar case and an ordinary deferred-field control, plus a Library NEWS entry.

Validation on macOS arm64 using the existing --with-pydebug interpreter built at dc28c3a358221b1e0ae5f926b72e112cbd1d03f7 (CPython 3.16.0a1+dev):

  • The original three-module reproducer raises the reported missing-__post_init__-argument error before the fix.
  • Against unchanged upstream dataclasses.py, the regression has three InitVar subcase errors and one ClassVar assertion failure; the ordinary deferred-field control passes. All five subcases pass after the fix.
  • ./python.exe -B -m test -u cpu --timeout 600 test_dataclasses test_annotationlib test_typing test_lazy_import test_inspect: all five suites succeed; reported run=1,708, skipped=1.
  • ./python.exe -B -m test -R 3:3 -m test_lazy_import_forwardref_annotations test_dataclasses: success, with no positive leaks in the three measured repetitions.
  • Unmodified Tools/patchcheck/patchcheck.py, scoped ruff check --no-fix --no-cache on the four changed Python files, git diff --cached --check and git diff --check: pass.

Ruff was the existing 0.15.22 binary, with its Python-3.15-support warning retained. The hook pins 0.15.17; the full hook runner was not installed or run. An initial attempt with another installed Ruff 0.14.6 could not parse the repository's Python 3.15 target configuration; the successful check used 0.15.22 without changing that configuration.

The dataclasses source and complete owning test tree, annotationlib source/tests, typing source/tests, inspect source and lazy-import test tree match main checked at 2468c35c3b3375fe0745fa5b5429d47098ebb45e. That main is 28 commits ahead of the validation baseline; the compared test-support change concerns sanitizer configuration detection. Later native changes and the entire CPython test suite were not validated locally. No new native build or dependency installation was performed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy imports break dataclass InitVars in extremely rare scenarios

1 participant