Skip to content

Respect lazy modes in parent import deduplication - #456

Closed
xiehuanyi wants to merge 2 commits into
asottile:mainfrom
xiehuanyi:fix/lazy-parent-module-deduplication-20261014
Closed

xiehuanyi wants to merge 2 commits into
asottile:mainfrom
xiehuanyi:fix/lazy-parent-module-deduplication-20261014

Conversation

@xiehuanyi

Copy link
Copy Markdown

On Python 3.15, import package followed by lazy import package.child is rewritten to just the lazy child import. The original source immediately loads the parent package. The rewritten source defers it, so initialization that the caller requested no longer runs at that point.

Include the declared lazy mode when tracking imported ancestor modules. The exact duplicate check already does this. Keep the existing parent deduplication for imports with the same mode. Also give the existing lazy-keyword helper a version-specific TypedDict, so nullable Python 3.15 AST flags typecheck without being mistaken for unrelated AST metadata fields.

Related to the lazy import support in #443.

Validation:

  • Five new regression cases fail on unchanged main; two same-mode controls pass.
  • A fresh Python process confirms that the parent package loads before and after the fix. The unchanged rewrite fails this check.
  • Python 3.15: 169 tests pass, one existing expected failure, 100% coverage.
  • Python 3.11: 155 tests pass, 14 skips, one existing expected failure, 100% coverage.
  • All configured checks pass, including mypy under both Python versions.

This was found during review of the ancestor-module duplicate check. AI assistance: OpenAI gpt-6.1-sol, xhigh, through Codex.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Repository owner locked as spam and limited conversation to collaborators Oct 14, 2026
@asottile asottile closed this Oct 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants