Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-82129: Provide __annotate__ method for dataclasses from make_dataclass#122262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
10 commits Select commit Hold shift + click to select a range
c2d85b7 gh-82128: Provide `__annotate__` method for dataclasses from `make_da…
sobolevn de409ad Merge branch 'main' into issue-82129-2
sobolevn 642f849 Fix NameError
sobolevn 69251ac Merge branch 'main' into issue-82129-2
sobolevn 5ec3a08 Address review
sobolevn 43caa3f Update Misc/NEWS.d/next/Library/2024-09-18-09-15-40.gh-issue-82129.GQ…
sobolevn 33b279d Introduce `_ANY_MARKER`
sobolevn d5ddb16 Merge branch 'main' into issue-82129-2
sobolevn 3b26e36 WIP
sobolevn 2c4d618 Merge branch 'main' into issue-82129-2
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -244,6 +244,10 @@ def __repr__(self): | ||
| property, | ||
| }) | ||
| # Any marker is used in `make_dataclass` to mark unannotated fields as `Any` | ||
| # without importing `typing` module. | ||
| _ANY_MARKER = object() | ||
| class InitVar: | ||
| __slots__ = ('type', ) | ||
| @@ -1591,7 +1595,7 @@ class C(Base): | ||
| for item in fields: | ||
| if isinstance(item, str): | ||
| name = item | ||
| tp = 'typing.Any' | ||
| tp = _ANY_MARKER | ||
| elif len(item) == 2: | ||
| name, tp, = item | ||
| elif len(item) == 3: | ||
| @@ -1610,11 +1614,29 @@ class C(Base): | ||
| seen.add(name) | ||
| annotations[name] = tp | ||
| def annotate_method(format): | ||
| typing = sys.modules.get("typing") | ||
| if typing is None and format == annotationlib.Format.FORWARDREF: | ||
| typing_any = annotationlib.ForwardRef("Any", module="typing") | ||
| return{ | ||
| ann: typing_any if t is _ANY_MARKER else t | ||
| for ann, t in annotations.items() | ||
| } | ||
| from typing import Any | ||
| ann_dict ={ | ||
| ann: Any if t is _ANY_MARKER else t | ||
| for ann, t in annotations.items() | ||
| } | ||
| if format == annotationlib.Format.STRING: | ||
| return annotationlib.annotations_to_string(ann_dict) | ||
| return ann_dict | ||
sobolevn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| # Update 'ns' with the user-supplied namespace plus our calculated values. | ||
| def exec_body_callback(ns): | ||
| ns['__annotate__'] = annotate_method | ||
| ns.update(namespace) | ||
| ns.update(defaults) | ||
| ns['__annotations__'] = annotations | ||
| # We use `types.new_class()` instead of simply `type()` to allow dynamic creation | ||
| # of generic dataclasses. | ||
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
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2024-09-18-09-15-40.gh-issue-82129.GQwt3u.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix :exc:`NameError` when calling :func:`typing.get_type_hints` on a :func:`dataclasses.dataclass` created by | ||
| :func:`dataclasses.make_dataclass` with un-annotated fields. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also avoid importing typing for the SOURCE format here I think; is that worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure we can. I need
_convert_to_source, there can be complex annotations that should be formatted properly. I will open a new issue about converting annotations to string with public API though. Right now I don't see a clear way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #124412 for that.