Skip to content

Commit c2d85b7

Browse files
committed
pythongh-82128: Provide __annotate__ method for dataclasses from make_dataclass
1 parent a3327db commit c2d85b7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Lib/dataclasses.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,10 +1527,11 @@ class C(Base):
15271527
seen = set()
15281528
annotations = {}
15291529
defaults = {}
1530+
any_marker = object()
15301531
for item in fields:
15311532
if isinstance(item, str):
15321533
name = item
1533-
tp = 'typing.Any'
1534+
tp = any_marker
15341535
elif len(item) == 2:
15351536
name, tp, = item
15361537
elif len(item) == 3:
@@ -1549,11 +1550,20 @@ class C(Base):
15491550
seen.add(name)
15501551
annotations[name] = tp
15511552

1553+
def annotate_method(format):
1554+
if format != 1:
1555+
raise NotImplementedError
1556+
from typing import Any
1557+
return {
1558+
ann: Any if t is any_marker else t
1559+
for ann, t in annotations.items()
1560+
}
1561+
15521562
# Update 'ns' with the user-supplied namespace plus our calculated values.
15531563
def exec_body_callback(ns):
1564+
ns['__annotate__'] = annotate_method
15541565
ns.update(namespace)
15551566
ns.update(defaults)
1556-
ns['__annotations__'] = annotations
15571567

15581568
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
15591569
# of generic dataclasses.

0 commit comments

Comments
 (0)