| 1 |
import textwrap |
| 2 |
import unittest |
| 3 |
from test.support import run_code |
| 4 |
|
| 5 |
class TypeAnnotationTests(unittest.TestCase): |
| 6 |
|
| 7 |
def test_lazy_create_annotations(self): |
| 8 |
# type objects lazy create their __annotations__ dict on demand. |
| 9 |
# the annotations dict is stored in type.__dict__. |
| 10 |
# a freshly created type shouldn't have an annotations dict yet. |
| 11 |
foo = type("Foo", (), {}) |
| 12 |
for i in range(3): |
| 13 |
self.assertFalse("__annotations__" in foo.__dict__) |
| 14 |
d = foo.__annotations__ |
| 15 |
self.assertTrue("__annotations__" in foo.__dict__) |
| 16 |
self.assertEqual(foo.__annotations__, d) |
| 17 |
self.assertEqual(foo.__dict__['__annotations__'], d) |
| 18 |
del foo.__annotations__ |
| 19 |
|
| 20 |
def test_setting_annotations(self): |
| 21 |
foo = type("Foo", (), {}) |
| 22 |
for i in range(3): |
| 23 |
self.assertFalse("__annotations__" in foo.__dict__) |
| 24 |
d = {'a': int} |
| 25 |
foo.__annotations__ = d |
| 26 |
self.assertTrue("__annotations__" in foo.__dict__) |
| 27 |
self.assertEqual(foo.__annotations__, d) |
| 28 |
self.assertEqual(foo.__dict__['__annotations__'], d) |
| 29 |
del foo.__annotations__ |
| 30 |
|
| 31 |
def test_annotations_getset_raises(self): |
| 32 |
# builtin types don't have __annotations__ (yet!) |
| 33 |
with self.assertRaises(AttributeError): |
| 34 |
print(float.__annotations__) |
| 35 |
with self.assertRaises(TypeError): |
| 36 |
float.__annotations__ = {} |
| 37 |
with self.assertRaises(TypeError): |
| 38 |
del float.__annotations__ |
| 39 |
|
| 40 |
# double delete |
| 41 |
foo = type("Foo", (), {}) |
| 42 |
foo.__annotations__ = {} |
| 43 |
del foo.__annotations__ |
| 44 |
with self.assertRaises(AttributeError): |
| 45 |
del foo.__annotations__ |
| 46 |
|
| 47 |
def test_annotations_are_created_correctly(self): |
| 48 |
class C: |
| 49 |
a:int=3 |
| 50 |
b:str=4 |
| 51 |
self.assertTrue("__annotations__" in C.__dict__) |
| 52 |
del C.__annotations__ |
| 53 |
self.assertFalse("__annotations__" in C.__dict__) |
| 54 |
|
| 55 |
def test_descriptor_still_works(self): |
| 56 |
class C: |
| 57 |
def __init__(self, name=None, bases=None, d=None): |
| 58 |
self.my_annotations = None |
| 59 |
|
| 60 |
@property |
| 61 |
def __annotations__(self): |
| 62 |
if not hasattr(self, 'my_annotations'): |
| 63 |
self.my_annotations = {} |
| 64 |
if not isinstance(self.my_annotations, dict): |
| 65 |
self.my_annotations = {} |
| 66 |
return self.my_annotations |
| 67 |
|
| 68 |
@__annotations__.setter |
| 69 |
def __annotations__(self, value): |
| 70 |
if not isinstance(value, dict): |
| 71 |
raise ValueError("can only set __annotations__ to a dict") |
| 72 |
self.my_annotations = value |
| 73 |
|
| 74 |
@__annotations__.deleter |
| 75 |
def __annotations__(self): |
| 76 |
if getattr(self, 'my_annotations', False) is None: |
| 77 |
raise AttributeError('__annotations__') |
| 78 |
self.my_annotations = None |
| 79 |
|
| 80 |
c = C() |
| 81 |
self.assertEqual(c.__annotations__, {}) |
| 82 |
d = {'a':'int'} |
| 83 |
c.__annotations__ = d |
| 84 |
self.assertEqual(c.__annotations__, d) |
| 85 |
with self.assertRaises(ValueError): |
| 86 |
c.__annotations__ = 123 |
| 87 |
del c.__annotations__ |
| 88 |
with self.assertRaises(AttributeError): |
| 89 |
del c.__annotations__ |
| 90 |
self.assertEqual(c.__annotations__, {}) |
| 91 |
|
| 92 |
|
| 93 |
class D(metaclass=C): |
| 94 |
pass |
| 95 |
|
| 96 |
self.assertEqual(D.__annotations__, {}) |
| 97 |
d = {'a':'int'} |
| 98 |
D.__annotations__ = d |
| 99 |
self.assertEqual(D.__annotations__, d) |
| 100 |
with self.assertRaises(ValueError): |
| 101 |
D.__annotations__ = 123 |
| 102 |
del D.__annotations__ |
| 103 |
with self.assertRaises(AttributeError): |
| 104 |
del D.__annotations__ |
| 105 |
self.assertEqual(D.__annotations__, {}) |
| 106 |
|
| 107 |
|
| 108 |
class TestSetupAnnotations(unittest.TestCase): |
| 109 |
def check(self, code: str): |
| 110 |
code = textwrap.dedent(code) |
| 111 |
for scope in ("module", "class"): |
| 112 |
with self.subTest(scope=scope): |
| 113 |
if scope == "class": |
| 114 |
code = f"class C:\n{textwrap.indent(code, ' ')}" |
| 115 |
ns = run_code(code) |
| 116 |
if scope == "class": |
| 117 |
annotations = ns["C"].__annotations__ |
| 118 |
else: |
| 119 |
annotations = ns["__annotations__"] |
| 120 |
self.assertEqual(annotations, {"x": int}) |
| 121 |
|
| 122 |
def test_top_level(self): |
| 123 |
self.check("x: int = 1") |
| 124 |
|
| 125 |
def test_blocks(self): |
| 126 |
self.check("if True:\n x: int = 1") |
| 127 |
self.check(""" |
| 128 |
while True: |
| 129 |
x: int = 1 |
| 130 |
break |
| 131 |
""") |
| 132 |
self.check(""" |
| 133 |
while False: |
| 134 |
pass |
| 135 |
else: |
| 136 |
x: int = 1 |
| 137 |
""") |
| 138 |
self.check(""" |
| 139 |
for i in range(1): |
| 140 |
x: int = 1 |
| 141 |
""") |
| 142 |
self.check(""" |
| 143 |
for i in range(1): |
| 144 |
pass |
| 145 |
else: |
| 146 |
x: int = 1 |
| 147 |
""") |
| 148 |
|
| 149 |
def test_try(self): |
| 150 |
self.check(""" |
| 151 |
try: |
| 152 |
x: int = 1 |
| 153 |
except: |
| 154 |
pass |
| 155 |
""") |
| 156 |
self.check(""" |
| 157 |
try: |
| 158 |
pass |
| 159 |
except: |
| 160 |
pass |
| 161 |
else: |
| 162 |
x: int = 1 |
| 163 |
""") |
| 164 |
self.check(""" |
| 165 |
try: |
| 166 |
pass |
| 167 |
except: |
| 168 |
pass |
| 169 |
finally: |
| 170 |
x: int = 1 |
| 171 |
""") |
| 172 |
self.check(""" |
| 173 |
try: |
| 174 |
1/0 |
| 175 |
except: |
| 176 |
x: int = 1 |
| 177 |
""") |
| 178 |
|
| 179 |
def test_try_star(self): |
| 180 |
self.check(""" |
| 181 |
try: |
| 182 |
x: int = 1 |
| 183 |
except* Exception: |
| 184 |
pass |
| 185 |
""") |
| 186 |
self.check(""" |
| 187 |
try: |
| 188 |
pass |
| 189 |
except* Exception: |
| 190 |
pass |
| 191 |
else: |
| 192 |
x: int = 1 |
| 193 |
""") |
| 194 |
self.check(""" |
| 195 |
try: |
| 196 |
pass |
| 197 |
except* Exception: |
| 198 |
pass |
| 199 |
finally: |
| 200 |
x: int = 1 |
| 201 |
""") |
| 202 |
self.check(""" |
| 203 |
try: |
| 204 |
1/0 |
| 205 |
except* Exception: |
| 206 |
x: int = 1 |
| 207 |
""") |
| 208 |
|
| 209 |
def test_match(self): |
| 210 |
self.check(""" |
| 211 |
match 0: |
| 212 |
case 0: |
| 213 |
x: int = 1 |
| 214 |
""") |