Skip to content

gh-158936: Resolve lazy imports consistently for dataclass annotations - #158939

Merged
JelleZijlstra merged 1 commit into
python:mainfrom
brittanyrey:b-dataclass-lazy-annotation
Oct 7, 2026
Merged

JelleZijlstra merged 1 commit into
python:mainfrom
brittanyrey:b-dataclass-lazy-annotation

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Before this change, dataclasses would handle the failure case for lazily imported annotations differently based on if that annotation was materialized as a string or an object. For objects, imports that could not be materialized were ignored, but for strings the errors would bubble up and completely prevent the initialization of the dataclass.

After this change, string annotations will resolve lazy imports the same way that annotationlib does: try to resolve them, and treat a failure as a missing annotation.

… like annotationlib

With string annotations, dataclasses looks up the first name of each
annotation to detect ClassVar, InitVar and KW_ONLY. If that name was a
lazy import that could not be resolved, the lookup raised and the class
could not be created, while the same class works with non-string
annotations.

This resolves lazy imports the way annotationlib does: try to resolve
them, and treat a failure like a missing name.
@JelleZijlstra JelleZijlstra added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Oct 7, 2026
@JelleZijlstra
JelleZijlstra merged commit 0f1d4a7 into python:main Oct 7, 2026
60 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @brittanyrey for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 7, 2026

Copy link
Copy Markdown

GH-158946 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Oct 7, 2026
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.

2 participants