Repository navigation
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
Changes from 6 commits
c2d85b7
de409ad
642f849
69251ac
5ec3a08
43caa3f
33b279d
d5ddb16
3b26e36
2c4d618
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1527,10 +1527,11 @@ class C(Base): | |
| seen = set() | ||
| annotations = {} | ||
| defaults = {} | ||
| any_marker = object() | ||
| 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: | ||
|
|
@@ -1549,11 +1550,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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we can. I need
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened #124412 for that. |
||
| 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, _convert_to_source | ||
| ann_dict = { | ||
| ann: Any if t is any_marker else t | ||
| for ann, t in annotations.items() | ||
| } | ||
| if format == annotationlib.Format.SOURCE: | ||
| return _convert_to_source(ann_dict) | ||
| return ann_dict | ||
|
|
||
|
sobolevn marked this conversation as resolved.
|
||
| # 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. | ||
|
|
||
| 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. |
Uh oh!
There was an error while loading. Please reload this page.