Repository navigation
gh-158936: Resolve lazy imports consistently for dataclass annotations - #158939
Merged
JelleZijlstra merged 1 commit intoOct 7, 2026
Merged
Conversation
… 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
approved these changes
Oct 7, 2026
|
Thanks @brittanyrey for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-158946 is a backport of this pull request to the 3.15 branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
annotationlibdoes: try to resolve them, and treat a failure as a missing annotation.