|
13 | 13 | import weakref
|
14 | 14 | import traceback
|
15 | 15 | import unittest
|
| 16 | +import sys |
16 | 17 | from unittest.mock import Mock
|
17 | 18 | from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
|
18 | 19 | from typing import get_type_hints
|
|
23 | 24 | import dataclasses # Needed for the string "dataclasses.InitVar[int]" to work as an annotation.
|
24 | 25 |
|
25 | 26 | from test import support
|
| 27 | +from test.support import import_helper |
26 | 28 |
|
27 | 29 | # Just any custom exception we can catch.
|
28 | 30 | class CustomError(Exception): pass
|
@@ -4108,16 +4110,27 @@ def test_no_types(self):
|
4108 | 4110 | C = make_dataclass('Point', ['x', 'y', 'z'])
|
4109 | 4111 | c = C(1, 2, 3)
|
4110 | 4112 | self.assertEqual(vars(c), {'x': 1, 'y': 2, 'z': 3})
|
4111 |
| - self.assertEqual(C.__annotations__, {'x': 'typing.Any', |
4112 |
| - 'y': 'typing.Any', |
4113 |
| - 'z': 'typing.Any'}) |
| 4113 | + self.assertEqual(C.__annotations__, {'x': typing.Any, |
| 4114 | + 'y': typing.Any, |
| 4115 | + 'z': typing.Any}) |
4114 | 4116 |
|
4115 | 4117 | C = make_dataclass('Point', ['x', ('y', int), 'z'])
|
4116 | 4118 | c = C(1, 2, 3)
|
4117 | 4119 | self.assertEqual(vars(c), {'x': 1, 'y': 2, 'z': 3})
|
4118 |
| - self.assertEqual(C.__annotations__, {'x': 'typing.Any', |
| 4120 | + self.assertEqual(C.__annotations__, {'x': typing.Any, |
4119 | 4121 | 'y': int,
|
4120 |
| - 'z': 'typing.Any'}) |
| 4122 | + 'z': typing.Any}) |
| 4123 | + |
| 4124 | + def test_no_types_no_NameError(self): |
| 4125 | + C = make_dataclass('Point', ['x']) |
| 4126 | + self.assertEqual(C.__annotations__, {'x': typing.Any}) |
| 4127 | + self.assertEqual(get_type_hints(C), {'x': typing.Any}) |
| 4128 | + |
| 4129 | + def test_no_types_no_typing_fallback(self): |
| 4130 | + with import_helper.isolated_modules(): |
| 4131 | + del sys.modules['typing'] |
| 4132 | + C = make_dataclass('Point', ['x']) |
| 4133 | + self.assertEqual(C.__annotations__, {'x': 'typing.Any'}) |
4121 | 4134 |
|
4122 | 4135 | def test_module_attr(self):
|
4123 | 4136 | self.assertEqual(ByMakeDataClass.__module__, __name__)
|
|
0 commit comments