| 1 |
import annotationlib |
| 2 |
import inspect |
| 3 |
import textwrap |
| 4 |
import types |
| 5 |
import unittest |
| 6 |
from test.support import run_code, check_syntax_error, import_helper, cpython_only |
| 7 |
from test.test_inspect import inspect_stringized_annotations |
| 8 |
|
| 9 |
|
| 10 |
class TypeAnnotationTests(unittest.TestCase): |
| 11 |
|
| 12 |
def test_lazy_create_annotations(self): |
| 13 |
# type objects lazy create their __annotations__ dict on demand. |
| 14 |
# the annotations dict is stored in type.__dict__ (as __annotations_cache__). |
| 15 |
# a freshly created type shouldn't have an annotations dict yet. |
| 16 |
foo = type("Foo", (), {}) |
| 17 |
for i in range(3): |
| 18 |
self.assertFalse("__annotations_cache__" in foo.__dict__) |
| 19 |
d = foo.__annotations__ |
| 20 |
self.assertTrue("__annotations_cache__" in foo.__dict__) |
| 21 |
self.assertEqual(foo.__annotations__, d) |
| 22 |
self.assertEqual(foo.__dict__['__annotations_cache__'], d) |
| 23 |
del foo.__annotations__ |
| 24 |
|
| 25 |
def test_setting_annotations(self): |
| 26 |
foo = type("Foo", (), {}) |
| 27 |
for i in range(3): |
| 28 |
self.assertFalse("__annotations_cache__" in foo.__dict__) |
| 29 |
d = {'a': int} |
| 30 |
foo.__annotations__ = d |
| 31 |
self.assertTrue("__annotations_cache__" in foo.__dict__) |
| 32 |
self.assertEqual(foo.__annotations__, d) |
| 33 |
self.assertEqual(foo.__dict__['__annotations_cache__'], d) |
| 34 |
del foo.__annotations__ |
| 35 |
|
| 36 |
def test_annotations_getset_raises(self): |
| 37 |
# builtin types don't have __annotations__ (yet!) |
| 38 |
with self.assertRaises(AttributeError): |
| 39 |
print(float.__annotations__) |
| 40 |
with self.assertRaises(TypeError): |
| 41 |
float.__annotations__ = {} |
| 42 |
with self.assertRaises(TypeError): |
| 43 |
del float.__annotations__ |
| 44 |
|
| 45 |
# double delete |
| 46 |
foo = type("Foo", (), {}) |
| 47 |
foo.__annotations__ = {} |
| 48 |
del foo.__annotations__ |
| 49 |
with self.assertRaises(AttributeError): |
| 50 |
del foo.__annotations__ |
| 51 |
|
| 52 |
def test_annotations_are_created_correctly(self): |
| 53 |
class C: |
| 54 |
a:int=3 |
| 55 |
b:str=4 |
| 56 |
self.assertEqual(C.__annotations__, {"a": int, "b": str}) |
| 57 |
self.assertTrue("__annotations_cache__" in C.__dict__) |
| 58 |
del C.__annotations__ |
| 59 |
self.assertFalse("__annotations_cache__" in C.__dict__) |
| 60 |
|
| 61 |
def test_pep563_annotations(self): |
| 62 |
isa = inspect_stringized_annotations |
| 63 |
self.assertEqual( |
| 64 |
isa.__annotations__, {"a": "int", "b": "str"}, |
| 65 |
) |
| 66 |
self.assertEqual( |
| 67 |
isa.MyClass.__annotations__, {"a": "int", "b": "str"}, |
| 68 |
) |
| 69 |
|
| 70 |
def test_explicitly_set_annotations(self): |
| 71 |
class C: |
| 72 |
__annotations__ = {"what": int} |
| 73 |
self.assertEqual(C.__annotations__, {"what": int}) |
| 74 |
|
| 75 |
def test_explicitly_set_annotate(self): |
| 76 |
class C: |
| 77 |
__annotate__ = lambda format: {"what": int} |
| 78 |
self.assertEqual(C.__annotations__, {"what": int}) |
| 79 |
self.assertIsInstance(C.__annotate__, types.FunctionType) |
| 80 |
self.assertEqual(C.__annotate__(annotationlib.Format.VALUE), {"what": int}) |
| 81 |
|
| 82 |
def test_del_annotations_and_annotate(self): |
| 83 |
# gh-132285 |
| 84 |
called = False |
| 85 |
class A: |
| 86 |
def __annotate__(format): |
| 87 |
nonlocal called |
| 88 |
called = True |
| 89 |
return {'a': int} |
| 90 |
|
| 91 |
self.assertEqual(A.__annotations__, {'a': int}) |
| 92 |
self.assertTrue(called) |
| 93 |
self.assertTrue(A.__annotate__) |
| 94 |
|
| 95 |
del A.__annotations__ |
| 96 |
called = False |
| 97 |
|
| 98 |
self.assertEqual(A.__annotations__, {}) |
| 99 |
self.assertFalse(called) |
| 100 |
self.assertIs(A.__annotate__, None) |
| 101 |
|
| 102 |
def test_descriptor_still_works(self): |
| 103 |
class C: |
| 104 |
def __init__(self, name=None, bases=None, d=None): |
| 105 |
self.my_annotations = None |
| 106 |
|
| 107 |
@property |
| 108 |
def __annotations__(self): |
| 109 |
if not hasattr(self, 'my_annotations'): |
| 110 |
self.my_annotations = {} |
| 111 |
if not isinstance(self.my_annotations, dict): |
| 112 |
self.my_annotations = {} |
| 113 |
return self.my_annotations |
| 114 |
|
| 115 |
@__annotations__.setter |
| 116 |
def __annotations__(self, value): |
| 117 |
if not isinstance(value, dict): |
| 118 |
raise ValueError("can only set __annotations__ to a dict") |
| 119 |
self.my_annotations = value |
| 120 |
|
| 121 |
@__annotations__.deleter |
| 122 |
def __annotations__(self): |
| 123 |
if getattr(self, 'my_annotations', False) is None: |
| 124 |
raise AttributeError('__annotations__') |
| 125 |
self.my_annotations = None |
| 126 |
|
| 127 |
c = C() |
| 128 |
self.assertEqual(c.__annotations__, {}) |
| 129 |
d = {'a':'int'} |
| 130 |
c.__annotations__ = d |
| 131 |
self.assertEqual(c.__annotations__, d) |
| 132 |
with self.assertRaises(ValueError): |
| 133 |
c.__annotations__ = 123 |
| 134 |
del c.__annotations__ |
| 135 |
with self.assertRaises(AttributeError): |
| 136 |
del c.__annotations__ |
| 137 |
self.assertEqual(c.__annotations__, {}) |
| 138 |
|
| 139 |
|
| 140 |
class D(metaclass=C): |
| 141 |
pass |
| 142 |
|
| 143 |
self.assertEqual(D.__annotations__, {}) |
| 144 |
d = {'a':'int'} |
| 145 |
D.__annotations__ = d |
| 146 |
self.assertEqual(D.__annotations__, d) |
| 147 |
with self.assertRaises(ValueError): |
| 148 |
D.__annotations__ = 123 |
| 149 |
del D.__annotations__ |
| 150 |
with self.assertRaises(AttributeError): |
| 151 |
del D.__annotations__ |
| 152 |
self.assertEqual(D.__annotations__, {}) |
| 153 |
|
| 154 |
def test_partially_executed_module(self): |
| 155 |
partialexe = import_helper.import_fresh_module("test.typinganndata.partialexecution") |
| 156 |
self.assertEqual( |
| 157 |
partialexe.a.__annotations__, |
| 158 |
{"v1": int, "v2": int}, |
| 159 |
) |
| 160 |
self.assertEqual(partialexe.b.annos, {"v1": int}) |
| 161 |
|
| 162 |
@cpython_only |
| 163 |
def test_no_cell(self): |
| 164 |
# gh-130924: Test that uses of annotations in local scopes do not |
| 165 |
# create cell variables. |
| 166 |
def f(x): |
| 167 |
a: x |
| 168 |
return x |
| 169 |
|
| 170 |
self.assertEqual(f.__code__.co_cellvars, ()) |
| 171 |
|
| 172 |
|
| 173 |
def build_module(code: str, name: str = "top") -> types.ModuleType: |
| 174 |
ns = run_code(code) |
| 175 |
mod = types.ModuleType(name) |
| 176 |
mod.__dict__.update(ns) |
| 177 |
return mod |
| 178 |
|
| 179 |
|
| 180 |
class TestSetupAnnotations(unittest.TestCase): |
| 181 |
def check(self, code: str): |
| 182 |
code = textwrap.dedent(code) |
| 183 |
for scope in ("module", "class"): |
| 184 |
with self.subTest(scope=scope): |
| 185 |
if scope == "class": |
| 186 |
code = f"class C:\n{textwrap.indent(code, ' ')}" |
| 187 |
ns = run_code(code) |
| 188 |
annotations = ns["C"].__annotations__ |
| 189 |
else: |
| 190 |
annotations = build_module(code).__annotations__ |
| 191 |
self.assertEqual(annotations, {"x": int}) |
| 192 |
|
| 193 |
def test_top_level(self): |
| 194 |
self.check("x: int = 1") |
| 195 |
|
| 196 |
def test_blocks(self): |
| 197 |
self.check("if True:\n x: int = 1") |
| 198 |
self.check(""" |
| 199 |
while True: |
| 200 |
x: int = 1 |
| 201 |
break |
| 202 |
""") |
| 203 |
self.check(""" |
| 204 |
while False: |
| 205 |
pass |
| 206 |
else: |
| 207 |
x: int = 1 |
| 208 |
""") |
| 209 |
self.check(""" |
| 210 |
for i in range(1): |
| 211 |
x: int = 1 |
| 212 |
""") |
| 213 |
self.check(""" |
| 214 |
for i in range(1): |
| 215 |
pass |
| 216 |
else: |
| 217 |
x: int = 1 |
| 218 |
""") |
| 219 |
|
| 220 |
def test_try(self): |
| 221 |
self.check(""" |
| 222 |
try: |
| 223 |
x: int = 1 |
| 224 |
except: |
| 225 |
pass |
| 226 |
""") |
| 227 |
self.check(""" |
| 228 |
try: |
| 229 |
pass |
| 230 |
except: |
| 231 |
pass |
| 232 |
else: |
| 233 |
x: int = 1 |
| 234 |
""") |
| 235 |
self.check(""" |
| 236 |
try: |
| 237 |
pass |
| 238 |
except: |
| 239 |
pass |
| 240 |
finally: |
| 241 |
x: int = 1 |
| 242 |
""") |
| 243 |
self.check(""" |
| 244 |
try: |
| 245 |
1/0 |
| 246 |
except: |
| 247 |
x: int = 1 |
| 248 |
""") |
| 249 |
|
| 250 |
def test_try_star(self): |
| 251 |
self.check(""" |
| 252 |
try: |
| 253 |
x: int = 1 |
| 254 |
except* Exception: |
| 255 |
pass |
| 256 |
""") |
| 257 |
self.check(""" |
| 258 |
try: |
| 259 |
pass |
| 260 |
except* Exception: |
| 261 |
pass |
| 262 |
else: |
| 263 |
x: int = 1 |
| 264 |
""") |
| 265 |
self.check(""" |
| 266 |
try: |
| 267 |
pass |
| 268 |
except* Exception: |
| 269 |
pass |
| 270 |
finally: |
| 271 |
x: int = 1 |
| 272 |
""") |
| 273 |
self.check(""" |
| 274 |
try: |
| 275 |
1/0 |
| 276 |
except* Exception: |
| 277 |
x: int = 1 |
| 278 |
""") |
| 279 |
|
| 280 |
def test_match(self): |
| 281 |
self.check(""" |
| 282 |
match 0: |
| 283 |
case 0: |
| 284 |
x: int = 1 |
| 285 |
""") |
| 286 |
|
| 287 |
|
| 288 |
class AnnotateTests(unittest.TestCase): |
| 289 |
"""See PEP 649.""" |
| 290 |
def test_manual_annotate(self): |
| 291 |
def f(): |
| 292 |
pass |
| 293 |
mod = types.ModuleType("mod") |
| 294 |
class X: |
| 295 |
pass |
| 296 |
|
| 297 |
for obj in (f, mod, X): |
| 298 |
with self.subTest(obj=obj): |
| 299 |
self.check_annotations(obj) |
| 300 |
|
| 301 |
def check_annotations(self, f): |
| 302 |
self.assertEqual(f.__annotations__, {}) |
| 303 |
self.assertIs(f.__annotate__, None) |
| 304 |
|
| 305 |
with self.assertRaisesRegex(TypeError, "__annotate__ must be callable or None"): |
| 306 |
f.__annotate__ = 42 |
| 307 |
f.__annotate__ = lambda: 42 |
| 308 |
with self.assertRaisesRegex(TypeError, r"takes 0 positional arguments but 1 was given"): |
| 309 |
print(f.__annotations__) |
| 310 |
|
| 311 |
f.__annotate__ = lambda x: 42 |
| 312 |
with self.assertRaisesRegex(TypeError, r"__annotate__ returned non-dict of type 'int'"): |
| 313 |
print(f.__annotations__) |
| 314 |
|
| 315 |
f.__annotate__ = lambda x: {"x": x} |
| 316 |
self.assertEqual(f.__annotations__, {"x": 1}) |
| 317 |
|
| 318 |
# Setting annotate to None does not invalidate the cached __annotations__ |
| 319 |
f.__annotate__ = None |
| 320 |
self.assertEqual(f.__annotations__, {"x": 1}) |
| 321 |
|
| 322 |
# But setting it to a new callable does |
| 323 |
f.__annotate__ = lambda x: {"y": x} |
| 324 |
self.assertEqual(f.__annotations__, {"y": 1}) |
| 325 |
|
| 326 |
# Setting f.__annotations__ also clears __annotate__ |
| 327 |
f.__annotations__ = {"z": 43} |
| 328 |
self.assertIs(f.__annotate__, None) |
| 329 |
|
| 330 |
def test_user_defined_annotate(self): |
| 331 |
class X: |
| 332 |
a: int |
| 333 |
|
| 334 |
def __annotate__(format): |
| 335 |
return {"a": str} |
| 336 |
self.assertEqual(X.__annotate__(annotationlib.Format.VALUE), {"a": str}) |
| 337 |
self.assertEqual(annotationlib.get_annotations(X), {"a": str}) |
| 338 |
|
| 339 |
mod = build_module( |
| 340 |
""" |
| 341 |
a: int |
| 342 |
def __annotate__(format): |
| 343 |
return {"a": str} |
| 344 |
""" |
| 345 |
) |
| 346 |
self.assertEqual(mod.__annotate__(annotationlib.Format.VALUE), {"a": str}) |
| 347 |
self.assertEqual(annotationlib.get_annotations(mod), {"a": str}) |
| 348 |
|
| 349 |
|
| 350 |
class DeferredEvaluationTests(unittest.TestCase): |
| 351 |
def test_function(self): |
| 352 |
def func(x: undefined, /, y: undefined, *args: undefined, z: undefined, **kwargs: undefined) -> undefined: |
| 353 |
pass |
| 354 |
|
| 355 |
with self.assertRaises(NameError): |
| 356 |
func.__annotations__ |
| 357 |
|
| 358 |
undefined = 1 |
| 359 |
self.assertEqual(func.__annotations__, { |
| 360 |
"x": 1, |
| 361 |
"y": 1, |
| 362 |
"args": 1, |
| 363 |
"z": 1, |
| 364 |
"kwargs": 1, |
| 365 |
"return": 1, |
| 366 |
}) |
| 367 |
|
| 368 |
def test_async_function(self): |
| 369 |
async def func(x: undefined, /, y: undefined, *args: undefined, z: undefined, **kwargs: undefined) -> undefined: |
| 370 |
pass |
| 371 |
|
| 372 |
with self.assertRaises(NameError): |
| 373 |
func.__annotations__ |
| 374 |
|
| 375 |
undefined = 1 |
| 376 |
self.assertEqual(func.__annotations__, { |
| 377 |
"x": 1, |
| 378 |
"y": 1, |
| 379 |
"args": 1, |
| 380 |
"z": 1, |
| 381 |
"kwargs": 1, |
| 382 |
"return": 1, |
| 383 |
}) |
| 384 |
|
| 385 |
def test_class(self): |
| 386 |
class X: |
| 387 |
a: undefined |
| 388 |
|
| 389 |
with self.assertRaises(NameError): |
| 390 |
X.__annotations__ |
| 391 |
|
| 392 |
undefined = 1 |
| 393 |
self.assertEqual(X.__annotations__, {"a": 1}) |
| 394 |
|
| 395 |
def test_module(self): |
| 396 |
ns = run_code("x: undefined = 1") |
| 397 |
anno = ns["__annotate__"] |
| 398 |
with self.assertRaises(NotImplementedError): |
| 399 |
anno(3) |
| 400 |
|
| 401 |
with self.assertRaises(NameError): |
| 402 |
anno(1) |
| 403 |
|
| 404 |
ns["undefined"] = 1 |
| 405 |
self.assertEqual(anno(1), {"x": 1}) |
| 406 |
|
| 407 |
def test_class_scoping(self): |
| 408 |
class Outer: |
| 409 |
def meth(self, x: Nested): ... |
| 410 |
x: Nested |
| 411 |
class Nested: ... |
| 412 |
|
| 413 |
self.assertEqual(Outer.meth.__annotations__, {"x": Outer.Nested}) |
| 414 |
self.assertEqual(Outer.__annotations__, {"x": Outer.Nested}) |
| 415 |
|
| 416 |
def test_no_exotic_expressions(self): |
| 417 |
preludes = [ |
| 418 |
"", |
| 419 |
"class X:\n ", |
| 420 |
"def f():\n ", |
| 421 |
"async def f():\n ", |
| 422 |
] |
| 423 |
for prelude in preludes: |
| 424 |
with self.subTest(prelude=prelude): |
| 425 |
check_syntax_error(self, prelude + "def func(x: (yield)): ...", "yield expression cannot be used within an annotation") |
| 426 |
check_syntax_error(self, prelude + "def func(x: (yield from x)): ...", "yield expression cannot be used within an annotation") |
| 427 |
check_syntax_error(self, prelude + "def func(x: (y := 3)): ...", "named expression cannot be used within an annotation") |
| 428 |
check_syntax_error(self, prelude + "def func(x: (await 42)): ...", "await expression cannot be used within an annotation") |
| 429 |
check_syntax_error(self, prelude + "def func(x: [y async for y in x]): ...", "asynchronous comprehension outside of an asynchronous function") |
| 430 |
check_syntax_error(self, prelude + "def func(x: {y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function") |
| 431 |
check_syntax_error(self, prelude + "def func(x: {y: y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function") |
| 432 |
|
| 433 |
def test_no_exotic_expressions_in_unevaluated_annotations(self): |
| 434 |
preludes = [ |
| 435 |
"", |
| 436 |
"class X: ", |
| 437 |
"def f(): ", |
| 438 |
"async def f(): ", |
| 439 |
] |
| 440 |
for prelude in preludes: |
| 441 |
with self.subTest(prelude=prelude): |
| 442 |
check_syntax_error(self, prelude + "(x): (yield)", "yield expression cannot be used within an annotation") |
| 443 |
check_syntax_error(self, prelude + "(x): (yield from x)", "yield expression cannot be used within an annotation") |
| 444 |
check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation") |
| 445 |
check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation") |
| 446 |
check_syntax_error(self, prelude + "(x): (await 42)", "await expression cannot be used within an annotation") |
| 447 |
check_syntax_error(self, prelude + "(x): [y async for y in x]", "asynchronous comprehension outside of an asynchronous function") |
| 448 |
check_syntax_error(self, prelude + "(x): {y async for y in x}", "asynchronous comprehension outside of an asynchronous function") |
| 449 |
check_syntax_error(self, prelude + "(x): {y: y async for y in x}", "asynchronous comprehension outside of an asynchronous function") |
| 450 |
|
| 451 |
def test_ignore_non_simple_annotations(self): |
| 452 |
ns = run_code("class X: (y): int") |
| 453 |
self.assertEqual(ns["X"].__annotations__, {}) |
| 454 |
ns = run_code("class X: int.b: int") |
| 455 |
self.assertEqual(ns["X"].__annotations__, {}) |
| 456 |
ns = run_code("class X: int[str]: int") |
| 457 |
self.assertEqual(ns["X"].__annotations__, {}) |
| 458 |
|
| 459 |
def test_generated_annotate(self): |
| 460 |
def func(x: int): |
| 461 |
pass |
| 462 |
class X: |
| 463 |
x: int |
| 464 |
mod = build_module("x: int") |
| 465 |
for obj in (func, X, mod): |
| 466 |
with self.subTest(obj=obj): |
| 467 |
annotate = obj.__annotate__ |
| 468 |
self.assertIsInstance(annotate, types.FunctionType) |
| 469 |
self.assertEqual(annotate.__name__, "__annotate__") |
| 470 |
with self.assertRaises(NotImplementedError): |
| 471 |
annotate(annotationlib.Format.FORWARDREF) |
| 472 |
with self.assertRaises(NotImplementedError): |
| 473 |
annotate(annotationlib.Format.STRING) |
| 474 |
with self.assertRaises(TypeError): |
| 475 |
annotate(None) |
| 476 |
self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int}) |
| 477 |
|
| 478 |
sig = inspect.signature(annotate) |
| 479 |
self.assertEqual(sig, inspect.Signature([ |
| 480 |
inspect.Parameter("format", inspect.Parameter.POSITIONAL_ONLY) |
| 481 |
])) |
| 482 |
|
| 483 |
def test_comprehension_in_annotation(self): |
| 484 |
# This crashed in an earlier version of the code |
| 485 |
ns = run_code("x: [y for y in range(10)]") |
| 486 |
self.assertEqual(ns["__annotate__"](1), {"x": list(range(10))}) |
| 487 |
|
| 488 |
def test_class_annotation_dunder_classdict(self): |
| 489 |
ns = run_code(""" |
| 490 |
class C: |
| 491 |
__classdict__: int |
| 492 |
""") |
| 493 |
self.assertEqual(ns["C"].__annotations__, {"__classdict__": int}) |
| 494 |
|
| 495 |
def test_future_annotations(self): |
| 496 |
code = """ |
| 497 |
from __future__ import annotations |
| 498 |
|
| 499 |
def f(x: int) -> int: pass |
| 500 |
""" |
| 501 |
ns = run_code(code) |
| 502 |
f = ns["f"] |
| 503 |
self.assertIsInstance(f.__annotate__, types.FunctionType) |
| 504 |
annos = {"x": "int", "return": "int"} |
| 505 |
self.assertEqual(f.__annotate__(annotationlib.Format.VALUE), annos) |
| 506 |
self.assertEqual(f.__annotations__, annos) |
| 507 |
|
| 508 |
def test_set_annotations(self): |
| 509 |
function_code = textwrap.dedent(""" |
| 510 |
def f(x: int): |
| 511 |
pass |
| 512 |
""") |
| 513 |
class_code = textwrap.dedent(""" |
| 514 |
class f: |
| 515 |
x: int |
| 516 |
""") |
| 517 |
for future in (False, True): |
| 518 |
for label, code in (("function", function_code), ("class", class_code)): |
| 519 |
with self.subTest(future=future, label=label): |
| 520 |
if future: |
| 521 |
code = "from __future__ import annotations\n" + code |
| 522 |
ns = run_code(code) |
| 523 |
f = ns["f"] |
| 524 |
anno = "int" if future else int |
| 525 |
self.assertEqual(f.__annotations__, {"x": anno}) |
| 526 |
|
| 527 |
f.__annotations__ = {"x": str} |
| 528 |
self.assertEqual(f.__annotations__, {"x": str}) |
| 529 |
|
| 530 |
def test_name_clash_with_format(self): |
| 531 |
# this test would fail if __annotate__'s parameter was called "format" |
| 532 |
# during symbol table construction |
| 533 |
code = """ |
| 534 |
class format: pass |
| 535 |
|
| 536 |
def f(x: format): pass |
| 537 |
""" |
| 538 |
ns = run_code(code) |
| 539 |
f = ns["f"] |
| 540 |
self.assertEqual(f.__annotations__, {"x": ns["format"]}) |
| 541 |
|
| 542 |
code = """ |
| 543 |
class Outer: |
| 544 |
class format: pass |
| 545 |
|
| 546 |
def meth(self, x: format): ... |
| 547 |
""" |
| 548 |
ns = run_code(code) |
| 549 |
self.assertEqual(ns["Outer"].meth.__annotations__, {"x": ns["Outer"].format}) |
| 550 |
|
| 551 |
code = """ |
| 552 |
def f(format): |
| 553 |
def inner(x: format): pass |
| 554 |
return inner |
| 555 |
res = f("closure var") |
| 556 |
""" |
| 557 |
ns = run_code(code) |
| 558 |
self.assertEqual(ns["res"].__annotations__, {"x": "closure var"}) |
| 559 |
|
| 560 |
code = """ |
| 561 |
def f(x: format): |
| 562 |
pass |
| 563 |
""" |
| 564 |
ns = run_code(code) |
| 565 |
# picks up the format() builtin |
| 566 |
self.assertEqual(ns["f"].__annotations__, {"x": format}) |
| 567 |
|
| 568 |
code = """ |
| 569 |
def outer(): |
| 570 |
def f(x: format): |
| 571 |
pass |
| 572 |
if False: |
| 573 |
class format: pass |
| 574 |
return f |
| 575 |
f = outer() |
| 576 |
""" |
| 577 |
ns = run_code(code) |
| 578 |
with self.assertRaisesRegex( |
| 579 |
NameError, |
| 580 |
"cannot access free variable 'format' where it is not associated with a value in enclosing scope", |
| 581 |
): |
| 582 |
ns["f"].__annotations__ |
| 583 |
|
| 584 |
|
| 585 |
class ConditionalAnnotationTests(unittest.TestCase): |
| 586 |
def check_scopes(self, code, true_annos, false_annos): |
| 587 |
for scope in ("class", "module"): |
| 588 |
for (cond, expected) in ( |
| 589 |
# Constants (so code might get optimized out) |
| 590 |
(True, true_annos), (False, false_annos), |
| 591 |
# Non-constant expressions |
| 592 |
("not not len", true_annos), ("not len", false_annos), |
| 593 |
): |
| 594 |
with self.subTest(scope=scope, cond=cond): |
| 595 |
code_to_run = code.format(cond=cond) |
| 596 |
if scope == "class": |
| 597 |
code_to_run = "class Cls:\n" + textwrap.indent(textwrap.dedent(code_to_run), " " * 4) |
| 598 |
ns = run_code(code_to_run) |
| 599 |
if scope == "class": |
| 600 |
self.assertEqual(ns["Cls"].__annotations__, expected) |
| 601 |
else: |
| 602 |
self.assertEqual(ns["__annotate__"](annotationlib.Format.VALUE), |
| 603 |
expected) |
| 604 |
|
| 605 |
def test_with(self): |
| 606 |
code = """ |
| 607 |
class Swallower: |
| 608 |
def __enter__(self): |
| 609 |
pass |
| 610 |
|
| 611 |
def __exit__(self, *args): |
| 612 |
return True |
| 613 |
|
| 614 |
with Swallower(): |
| 615 |
if {cond}: |
| 616 |
about_to_raise: int |
| 617 |
raise Exception |
| 618 |
in_with: "with" |
| 619 |
""" |
| 620 |
self.check_scopes(code, {"about_to_raise": int}, {"in_with": "with"}) |
| 621 |
|
| 622 |
def test_simple_if(self): |
| 623 |
code = """ |
| 624 |
if {cond}: |
| 625 |
in_if: "if" |
| 626 |
else: |
| 627 |
in_if: "else" |
| 628 |
""" |
| 629 |
self.check_scopes(code, {"in_if": "if"}, {"in_if": "else"}) |
| 630 |
|
| 631 |
def test_if_elif(self): |
| 632 |
code = """ |
| 633 |
if not len: |
| 634 |
in_if: "if" |
| 635 |
elif {cond}: |
| 636 |
in_elif: "elif" |
| 637 |
else: |
| 638 |
in_else: "else" |
| 639 |
""" |
| 640 |
self.check_scopes( |
| 641 |
code, |
| 642 |
{"in_elif": "elif"}, |
| 643 |
{"in_else": "else"} |
| 644 |
) |
| 645 |
|
| 646 |
def test_try(self): |
| 647 |
code = """ |
| 648 |
try: |
| 649 |
if {cond}: |
| 650 |
raise Exception |
| 651 |
in_try: "try" |
| 652 |
except Exception: |
| 653 |
in_except: "except" |
| 654 |
finally: |
| 655 |
in_finally: "finally" |
| 656 |
""" |
| 657 |
self.check_scopes( |
| 658 |
code, |
| 659 |
{"in_except": "except", "in_finally": "finally"}, |
| 660 |
{"in_try": "try", "in_finally": "finally"} |
| 661 |
) |
| 662 |
|
| 663 |
def test_try_star(self): |
| 664 |
code = """ |
| 665 |
try: |
| 666 |
if {cond}: |
| 667 |
raise Exception |
| 668 |
in_try_star: "try" |
| 669 |
except* Exception: |
| 670 |
in_except_star: "except" |
| 671 |
finally: |
| 672 |
in_finally: "finally" |
| 673 |
""" |
| 674 |
self.check_scopes( |
| 675 |
code, |
| 676 |
{"in_except_star": "except", "in_finally": "finally"}, |
| 677 |
{"in_try_star": "try", "in_finally": "finally"} |
| 678 |
) |
| 679 |
|
| 680 |
def test_while(self): |
| 681 |
code = """ |
| 682 |
while {cond}: |
| 683 |
in_while: "while" |
| 684 |
break |
| 685 |
else: |
| 686 |
in_else: "else" |
| 687 |
""" |
| 688 |
self.check_scopes( |
| 689 |
code, |
| 690 |
{"in_while": "while"}, |
| 691 |
{"in_else": "else"} |
| 692 |
) |
| 693 |
|
| 694 |
def test_for(self): |
| 695 |
code = """ |
| 696 |
for _ in ([1] if {cond} else []): |
| 697 |
in_for: "for" |
| 698 |
else: |
| 699 |
in_else: "else" |
| 700 |
""" |
| 701 |
self.check_scopes( |
| 702 |
code, |
| 703 |
{"in_for": "for", "in_else": "else"}, |
| 704 |
{"in_else": "else"} |
| 705 |
) |
| 706 |
|
| 707 |
def test_match(self): |
| 708 |
code = """ |
| 709 |
match {cond}: |
| 710 |
case True: |
| 711 |
x: "true" |
| 712 |
case False: |
| 713 |
x: "false" |
| 714 |
""" |
| 715 |
self.check_scopes( |
| 716 |
code, |
| 717 |
{"x": "true"}, |
| 718 |
{"x": "false"} |
| 719 |
) |
| 720 |
|
| 721 |
def test_nesting_override(self): |
| 722 |
code = """ |
| 723 |
if {cond}: |
| 724 |
x: "foo" |
| 725 |
if {cond}: |
| 726 |
x: "bar" |
| 727 |
""" |
| 728 |
self.check_scopes( |
| 729 |
code, |
| 730 |
{"x": "bar"}, |
| 731 |
{} |
| 732 |
) |
| 733 |
|
| 734 |
def test_nesting_outer(self): |
| 735 |
code = """ |
| 736 |
if {cond}: |
| 737 |
outer_before: "outer_before" |
| 738 |
if len: |
| 739 |
inner_if: "inner_if" |
| 740 |
else: |
| 741 |
inner_else: "inner_else" |
| 742 |
outer_after: "outer_after" |
| 743 |
""" |
| 744 |
self.check_scopes( |
| 745 |
code, |
| 746 |
{"outer_before": "outer_before", "inner_if": "inner_if", |
| 747 |
"outer_after": "outer_after"}, |
| 748 |
{} |
| 749 |
) |
| 750 |
|
| 751 |
def test_nesting_inner(self): |
| 752 |
code = """ |
| 753 |
if len: |
| 754 |
outer_before: "outer_before" |
| 755 |
if {cond}: |
| 756 |
inner_if: "inner_if" |
| 757 |
else: |
| 758 |
inner_else: "inner_else" |
| 759 |
outer_after: "outer_after" |
| 760 |
""" |
| 761 |
self.check_scopes( |
| 762 |
code, |
| 763 |
{"outer_before": "outer_before", "inner_if": "inner_if", |
| 764 |
"outer_after": "outer_after"}, |
| 765 |
{"outer_before": "outer_before", "inner_else": "inner_else", |
| 766 |
"outer_after": "outer_after"}, |
| 767 |
) |
| 768 |
|
| 769 |
def test_non_name_annotations(self): |
| 770 |
code = """ |
| 771 |
before: "before" |
| 772 |
if {cond}: |
| 773 |
a = "x" |
| 774 |
a[0]: int |
| 775 |
else: |
| 776 |
a = object() |
| 777 |
a.b: str |
| 778 |
after: "after" |
| 779 |
""" |
| 780 |
expected = {"before": "before", "after": "after"} |
| 781 |
self.check_scopes(code, expected, expected) |
| 782 |
|
| 783 |
|
| 784 |
class RegressionTests(unittest.TestCase): |
| 785 |
# gh-132479 |
| 786 |
def test_complex_comprehension_inlining(self): |
| 787 |
# Test that the various repro cases from the issue don't crash |
| 788 |
cases = [ |
| 789 |
""" |
| 790 |
(unique_name_0): 0 |
| 791 |
unique_name_1: ( |
| 792 |
0 |
| 793 |
for ( |
| 794 |
0 |
| 795 |
for unique_name_2 in 0 |
| 796 |
for () in (0 for unique_name_3 in unique_name_4 for unique_name_5 in name_1) |
| 797 |
).name_3 in {0: 0 for name_1 in unique_name_8} |
| 798 |
if name_1 |
| 799 |
) |
| 800 |
""", |
| 801 |
""" |
| 802 |
unique_name_0: 0 |
| 803 |
unique_name_1: { |
| 804 |
0: 0 |
| 805 |
for unique_name_2 in [0 for name_0 in unique_name_4] |
| 806 |
if { |
| 807 |
0: 0 |
| 808 |
for unique_name_5 in 0 |
| 809 |
if name_0 |
| 810 |
if ((name_0 for unique_name_8 in unique_name_9) for [] in 0) |
| 811 |
} |
| 812 |
} |
| 813 |
""", |
| 814 |
""" |
| 815 |
0[0]: {0 for name_0 in unique_name_1} |
| 816 |
unique_name_2: { |
| 817 |
0: (lambda: name_0 for unique_name_4 in unique_name_5) |
| 818 |
for unique_name_6 in () |
| 819 |
if name_0 |
| 820 |
} |
| 821 |
""", |
| 822 |
] |
| 823 |
for case in cases: |
| 824 |
case = textwrap.dedent(case) |
| 825 |
compile(case, "<test>", "exec") |
| 826 |
|
| 827 |
def test_complex_comprehension_inlining_exec(self): |
| 828 |
code = """ |
| 829 |
unique_name_1 = unique_name_5 = [1] |
| 830 |
name_0 = 42 |
| 831 |
unique_name_7: {name_0 for name_0 in unique_name_1} |
| 832 |
unique_name_2: { |
| 833 |
0: (lambda: name_0 for unique_name_4 in unique_name_5) |
| 834 |
for unique_name_6 in [1] |
| 835 |
if name_0 |
| 836 |
} |
| 837 |
""" |
| 838 |
mod = build_module(code) |
| 839 |
annos = mod.__annotations__ |
| 840 |
self.assertEqual(annos.keys(), {"unique_name_7", "unique_name_2"}) |
| 841 |
self.assertEqual(annos["unique_name_7"], {True}) |
| 842 |
genexp = annos["unique_name_2"][0] |
| 843 |
lamb = list(genexp)[0] |
| 844 |
self.assertEqual(lamb(), 42) |
| 845 |
|
| 846 |
def test_annotate_qualname(self): |
| 847 |
code = """ |
| 848 |
def f() -> None: |
| 849 |
def nested() -> None: pass |
| 850 |
return nested |
| 851 |
class Outer: |
| 852 |
x: int |
| 853 |
def method(self, x: int): |
| 854 |
pass |
| 855 |
""" |
| 856 |
ns = run_code(code) |
| 857 |
method = ns["Outer"].method |
| 858 |
self.assertEqual(ns["f"].__annotate__.__qualname__, "f.__annotate__") |
| 859 |
self.assertEqual(ns["f"]().__annotate__.__qualname__, "f.<locals>.nested.__annotate__") |
| 860 |
self.assertEqual(method.__annotate__.__qualname__, "Outer.method.__annotate__") |
| 861 |
self.assertEqual(ns["Outer"].__annotate__.__qualname__, "Outer.__annotate__") |
| 862 |
|
| 863 |
# gh-138349 |
| 864 |
def test_module_level_annotation_plus_listcomp(self): |
| 865 |
cases = [ |
| 866 |
""" |
| 867 |
def report_error(): |
| 868 |
pass |
| 869 |
try: |
| 870 |
[0 for name_2 in unique_name_0 if (lambda: name_2)] |
| 871 |
except: |
| 872 |
pass |
| 873 |
annotated_name: 0 |
| 874 |
""", |
| 875 |
""" |
| 876 |
class Generic: |
| 877 |
pass |
| 878 |
try: |
| 879 |
[0 for name_2 in unique_name_0 if (0 for unique_name_1 in unique_name_2 for unique_name_3 in name_2)] |
| 880 |
except: |
| 881 |
pass |
| 882 |
annotated_name: 0 |
| 883 |
""", |
| 884 |
""" |
| 885 |
class Generic: |
| 886 |
pass |
| 887 |
annotated_name: 0 |
| 888 |
try: |
| 889 |
[0 for name_2 in [[0]] for unique_name_1 in unique_name_2 if (lambda: name_2)] |
| 890 |
except: |
| 891 |
pass |
| 892 |
""", |
| 893 |
] |
| 894 |
for code in cases: |
| 895 |
with self.subTest(code=code): |
| 896 |
mod = build_module(code) |
| 897 |
annos = mod.__annotations__ |
| 898 |
self.assertEqual(annos, {"annotated_name": 0}) |