bpo-34776: Fix dataclasses to support __future__ "annotations" mode (… · python/cpython@0d57db2 · GitHub
Skip to content

Commit 0d57db2

Browse files
miss-islingtonYury Selivanov
authored andcommitted
bpo-34776: Fix dataclasses to support __future__ "annotations" mode (GH-9518) (#17531)
(cherry picked from commit d219cc4) Co-authored-by: Yury Selivanov <yury@magic.io>
1 parent 79c2974 commit 0d57db2

4 files changed

Lines changed: 78 additions & 34 deletions

File tree

Lib/dataclasses.py

Lines changed: 53 additions & 34 deletions

Lib/test/dataclass_textanno.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from __future__ import annotations
2+
3+
import dataclasses
4+
5+
6+
class Foo:
7+
pass
8+
9+
10+
@dataclasses.dataclass
11+
class Bar:
12+
foo: Foo

Lib/test/test_dataclasses.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import unittest
1111
from unittest.mock import Mock
1212
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
13+
from typing import get_type_hints
1314
from collections import deque, OrderedDict, namedtuple
1415
from functools import total_ordering
1516

@@ -2926,6 +2927,17 @@ def test_classvar_module_level_import(self):
29262927
# won't exist on the instance.
29272928
self.assertNotIn('not_iv4', c.__dict__)
29282929

2930+
def test_text_annotations(self):
2931+
from test import dataclass_textanno
2932+
2933+
self.assertEqual(
2934+
get_type_hints(dataclass_textanno.Bar),
2935+
{'foo': dataclass_textanno.Foo})
2936+
self.assertEqual(
2937+
get_type_hints(dataclass_textanno.Bar.__init__),
2938+
{'foo': dataclass_textanno.Foo,
2939+
'return': type(None)})
2940+
29292941

29302942
class TestMakeDataclass(unittest.TestCase):
29312943
def test_simple(self):
Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)