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

Commit 66d7a5d

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

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

@@ -2918,6 +2919,17 @@ def test_classvar_module_level_import(self):
29182919
# won't exist on the instance.
29192920
self.assertNotIn('not_iv4', c.__dict__)
29202921

2922+
def test_text_annotations(self):
2923+
from test import dataclass_textanno
2924+
2925+
self.assertEqual(
2926+
get_type_hints(dataclass_textanno.Bar),
2927+
{'foo': dataclass_textanno.Foo})
2928+
self.assertEqual(
2929+
get_type_hints(dataclass_textanno.Bar.__init__),
2930+
{'foo': dataclass_textanno.Foo,
2931+
'return': type(None)})
2932+
29212933

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

0 commit comments

Comments
 (0)