File size: 67,793 Bytes
a4edccc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 | You are working in `/workspace/cpython`, a source tree checked out at
the base commit for this task. Implement the requested behavior in the source
tree, then run:
```bash
lolbench-submit
```
Do not stop after editing files, running tests, or describing the solution. The
task is complete only when `lolbench-submit` has created
`/logs/artifacts/solution.patch`. If that file does not exist, continue
working and run `lolbench-submit` again.
That command writes your implementation diff to
`/logs/artifacts/solution.patch`, which is the artifact the Harbor verifier
will grade. Before running `lolbench-submit`, clean or revert any test files
you created or modified; test files must not be included in the final
`solution.patch`.
This environment has no outbound internet access — `curl`/`wget`, `git fetch`/`clone`, package installs, and web fetch/search will all fail. Implement the requirements using only the code already in the workspace and your own knowledge; do not attempt to fetch or search external resources.
Now please implement the following requirements in the source tree:
---
********
Abstract
********
Annotations are a Python technology that allows expressing
type information and other metadata about Python functions,
classes, and modules. But Python's original semantics
for annotations required them to be eagerly evaluated,
at the time the annotated object was bound. This caused
chronic problems for static type analysis users using
"type hints", due to forward-reference and circular-reference
problems.
Python solved this by accepting PEP 563, incorporating
a new approach called "stringized annotations" in which
annotations were automatically converted into strings by
Python. This solved the forward-reference and circular-reference
problems, and also fostered intriguing new uses for annotation
metadata. But stringized annotations in turn caused chronic
problems for runtime users of annotations.
This PEP proposes a new and comprehensive third approach
for representing and computing annotations. It adds a new
internal mechanism for lazily computing annotations on demand,
via a new object method called `__annotate__`.
This approach, when combined with a novel technique for
coercing annotation values into alternative formats, solves
all the above problems, supports all existing use cases,
and should foster future innovations in annotations.
********
Overview
********
This PEP adds a new dunder attribute to the objects that
support annotations--functions, classes, and modules.
The new attribute is called `__annotate__`, and is
a reference to a function which computes and returns
that object's annotations dict.
At compile time, if the definition of an object includes
annotations, the Python compiler will write the expressions
computing the annotations into its own function. When run,
the function will return the annotations dict. The Python
compiler then stores a reference to this function in
`__annotate__` on the object.
Furthermore, `__annotations__` is redefined to be a
"data descriptor" which calls this annotation function once
and caches the result.
This mechanism delays the evaluation of annotations expressions
until the annotations are examined, which solves many circular
reference problems.
This PEP also defines new functionality for two functions
in the Python standard library:
`inspect.get_annotations` and `typing.get_type_hints`.
The functionality is accessed via a new keyword-only parameter,
`format`. `format` allows the user to request
the annotations from these functions
in a specific format.
Format identifiers are always predefined integer values.
The formats defined by this PEP are:
The standard-library implementation exposes these format identifiers as
`annotationlib.Format.VALUE`, `annotationlib.Format.FORWARDREF`, and
`annotationlib.Format.SOURCE`.
* `inspect.VALUE = 1`
The default value.
The function will return the conventional Python
values for the annotations. This format is identical
to the return value for these functions under Python 3.11.
* `inspect.FORWARDREF = 2`
The function will attempt to return the conventional
Python values for the annotations. However, if it
encounters an undefined name, or a free variable that
has not yet been associated with a value, it dynamically
creates a proxy object (a `ForwardRef`) that substitutes
for that value in the expression, then continues evaluation.
The resulting dict may contain a mixture of proxies and
real values. If all real values are defined at the time
the function is called, `inspect.FORWARDREF` and
`inspect.VALUE` produce identical results.
The public proxy type for these values is `annotationlib.ForwardRef`; its
`evaluate()` method evaluates the stored forward-reference expression when the
required names are available. A `ForwardRef` may also be constructed
directly, and its constructor accepts an `owner` keyword recording the
module, class, or function that owns the annotation; `evaluate()` consults
that owner to recover the globals, locals, and type parameters it needs to
resolve the expression. For callers that already hold a forward reference,
the `typing` module additionally exposes a top-level
`typing.evaluate_forward_ref()` function that drives the same evaluation.
* `inspect.SOURCE = 3`
The function will produce an annotation dictionary
where the values have been replaced by strings containing
the original source code for the annotation expressions.
These strings may only be approximate, as they may be
reverse-engineered from another format, rather than
preserving the original source code, but the differences
will be minor.
If accepted, this PEP would *supersede* PEP 563,
and PEP 563's behavior would be deprecated and
eventually removed.
## Comparison Of Annotation Semantics
> **Note:** The code presented in this section is simplified
for clarity, and is intentionally inaccurate in some
critical aspects. This example is intended merely to
communicate the high-level concepts involved without
getting lost in the details. But readers should note
that the actual implementation is quite different in
several important ways. See the Implementation_
section later in this PEP for a far more accurate
description of what this PEP proposes from a technical
level.
Consider this example code:
```
def foo(x: int = 3, y: MyType = None) -> float:
...
class MyType:
...
foo_y_annotation = foo.__annotations__['y']
```
As we see here, annotations are available at runtime through an
`__annotations__` attribute on functions, classes, and modules.
When annotations are specified on one of these objects,
`__annotations__` is a dictionary mapping the names of the
fields to the value specified as that field's annotation.
The default behavior in Python is to evaluate the expressions
for the annotations, and build the annotations dict, at the time
the function, class, or module is bound. At runtime the above
code actually works something like this:
```
annotations = {'x': int, 'y': MyType, 'return': float}
def foo(x = 3, y = "abc"):
...
foo.__annotations__ = annotations
class MyType:
...
foo_y_annotation = foo.__annotations__['y']
```
The crucial detail here is that the values `int`, `MyType`,
and `float` are looked up at the time the function object is
bound, and these values are stored in the annotations dict.
But this code doesn't run—it throws a `NameError` on the first
line, because `MyType` hasn't been defined yet.
PEP 563's solution is to decompile the expressions back
into strings during compilation and store those strings as the
values in the annotations dict. The equivalent runtime code
would look something like this:
```
annotations = {'x': 'int', 'y': 'MyType', 'return': 'float'}
def foo(x = 3, y = "abc"):
...
foo.__annotations__ = annotations
class MyType:
...
foo_y_annotation = foo.__annotations__['y']
```
This code now runs successfully. However, `foo_y_annotation`
is no longer a reference to `MyType`, it is the *string*
`'MyType'`. To turn the string into the real value `MyType`,
the user would need to evaluate the string using `eval`,
`inspect.get_annotations`, or `typing.get_type_hints`.
This PEP proposes a third approach, delaying the evaluation of
the annotations by computing them in their own function. If
this PEP was active, the generated code would work something
like this:
```
class function:
# __annotations__ on a function object is already a
# "data descriptor" in Python, we're just changing
# what it does
@property
def __annotations__(self):
return self.__annotate__()
## ...
def annotate_foo():
return {'x': int, 'y': MyType, 'return': float}
def foo(x = 3, y = "abc"):
...
foo.__annotate__ = annotate_foo
class MyType:
...
foo_y_annotation = foo.__annotations__['y']
```
The important change is that the code constructing the
annotations dict now lives in a function—here, called
`annotate_foo()`. But this function isn't called
until we ask for the value of `foo.__annotations__`,
and we don't do that until *after* the definition of `MyType`.
So this code also runs successfully, and `foo_y_annotation` now
has the correct value--the class `MyType`--even though
`MyType` wasn't defined until *after* the annotation was
defined.
## Mistaken Rejection Of This Approach In November 2017
During the early days of discussion around PEP 563,
in a November 2017 thread in `comp.lang.python-dev`,
the idea of using code to delay the evaluation of
annotations was briefly discussed. At the time the
technique was termed an "implicit lambda expression".
Guido van Rossum—Python's BDFL at the time—replied,
asserting that these "implicit lambda expression" wouldn't
work, because they'd only be able to resolve symbols at
module-level scope:
IMO the inability of referencing class-level definitions
from annotations on methods pretty much kills this idea.
https://mail.python.org/pipermail/python-dev/2017-November/150109.html
This led to a short discussion about extending lambda-ized
annotations for methods to be able to refer to class-level
definitions, by maintaining a reference to the class-level
scope. This idea, too, was quickly rejected.
:pep:`PEP 563 summarizes the above discussion
<563#keeping-the-ability-to-use-function-local-state-when-defining-annotations>`
The approach taken by this PEP doesn't suffer from these
restrictions. Annotations can access module-level definitions,
class-level definitions, and even local and free variables.
**********
Motivation
**********
## A History Of Annotations
Python 3.0 shipped with a new syntax feature, "annotations",
defined in PEP 3107.
This allowed specifying a Python value that would be
associated with a parameter of a Python function, or
with the value that function returns.
Said another way, annotations gave Python users an interface
to provide rich metadata about a function parameter or return
value, for example type information.
All the annotations for a function were stored together in
a new attribute `__annotations__`, in an "annotation dict"
that mapped parameter names (or, in the case of the return
annotation, using the name `'return'`) to their Python value.
In an effort to foster experimentation, Python
intentionally didn't define what form this metadata should take,
or what values should be used. User code began experimenting with
this new facility almost immediately. But popular libraries that
make use of this functionality were slow to emerge.
After years of little progress, the BDFL chose a particular
approach for expressing static type information, called
*type hints,* as defined in PEP 484. Python 3.5 shipped
with a new `typing` module which quickly became very popular.
Python 3.6 added syntax to annotate local variables,
class attributes, and module attributes, using the approach
proposed in PEP 526. Static type analysis continued to
grow in popularity.
However, static type analysis users were increasingly frustrated
by an inconvenient problem: forward references. In classic
Python, if a class C depends on a later-defined class D,
it's normally not a problem, because user code will usually
wait until both are defined before trying to use either.
But annotations added a new complication, because they were
computed at the time the annotated object (function, class,
or module) was bound. If methods on class C are annotated with
type D, and these annotation expressions are computed at the
time that the method is bound, D may not be defined yet.
And if methods in D are also annotated with type C, you now
have an unresolvable circular reference problem.
Initially, static type users worked around this problem
by defining their problematic annotations as strings.
This worked because a string containing the type hint was
just as usable for the static type analysis tool.
And users of static type analysis tools rarely examine the
annotations at runtime, so this representation wasn't
itself an inconvenience. But manually stringizing type
hints was clumsy and error-prone. Also, code bases were
adding more and more annotations, which consumed more and
more CPU time to create and bind.
To solve these problems, the BDFL accepted PEP 563, which
added a new feature to Python 3.7: "stringized annotations".
It was activated with a future import:
```
from __future__ import annotations
```
Normally, annotation expressions were evaluated at the time
the object was bound, with their values being stored in the
annotations dict. When stringized annotations were active,
these semantics changed: instead, at compile time, the compiler
converted all annotations in that module into string
representations of their source code--thus, *automatically*
turning the users's annotations into strings, obviating the
need to *manually* stringize them as before. PEP 563
suggested users could evaluate this string with `eval`
if the actual value was needed at runtime.
(From here on out, this PEP will refer to the classic
semantics of PEP 3107 and PEP 526, where the
values of annotation expressions are computed at the time
the object is bound, as *"stock" semantics,* to differentiate
them from the new PEP 563 "stringized" annotation semantics.)
## The Current State Of Annotation Use Cases
Although there are many specific use cases for annotations,
annotation users in the discussion around this PEP tended
to fall into one of these four categories.
### Static typing users
Static typing users use annotations to add type information
to their code. But they largely don't examine the annotations
at runtime. Instead, they use static type analysis tools
(mypy, pytype) to examine their source tree and determine
whether or not their code is using types consistently.
This is almost certainly the most popular use case for
annotations today.
Many of the annotations use *type hints,* a la PEP 484
(and many subsequent PEPs). Type hints are passive objects,
mere representation of type information; they don't do any actual work.
Type hints are often parameterized with other types or other type hints.
Since they're agnostic about what these actual values are, type hints
work fine with `ForwardRef` proxy objects.
Users of static type hints discovered that extensive type hinting under
stock semantics often created large-scale circular reference and circular
import problems that could be difficult to solve. PEP 563 was designed
specifically to solve this problem, and the solution worked great for
these users. The difficulty of rendering stringized annotations into
real values largely didn't inconvenience these users because of how
infrequently they examine annotations at runtime.
Static typing users often combine PEP 563 with the
`if typing.TYPE_CHECKING` idiom to prevent their type hints from being
loaded at runtime. This means they often aren't able to evaluate their
stringized annotations and produce real values at runtime. On the rare
occasion that they do examine annotations at runtime, they often forgo
`eval`, instead using lexical analysis directly on the stringized
annotations.
Under this PEP, static typing users will probably prefer `FORWARDREF`
or `SOURCE` format.
### Runtime annotation users
Runtime annotation users use annotations as a means of expressing rich
metadata about their functions and classes, which they use as input to
runtime behavior. Specific use cases include runtime type verification
(Pydantic) and glue logic to expose Python APIs in another domain
(FastAPI, Typer). The annotations may or may not be type hints.
As runtime annotation users examine annotations at runtime, they were
traditionally better served with stock semantics. This use case is
largely incompatible with PEP 563, particularly with the
`if typing.TYPE_CHECKING` idiom.
Under this PEP, runtime annotation users will most likely prefer `VALUE`
format, though some (e.g. if they evaluate annotations eagerly in a decorator
and want to support forward references) may also use `FORWARDREF` format.
### Wrappers
Wrappers are functions or classes that wrap user functions or
classes and add functionality. Examples of this would be
`~dataclasses.dataclass`, `functools.partial`,
`attrs`, and `wrapt`.
Wrappers are a distinct subcategory of runtime annotation users.
Although they do use annotations at runtime, they may or may not
actually examine the annotations of the objects they wrap--it depends
on the functionality the wrapper provides. As a rule they should
propagate the annotations of the wrapped object to the wrapper
they create, although it's possible they may modify those annotations.
Wrappers were generally designed to work well under stock semantics.
Whether or not they work well under PEP 563 semantics depends on the
degree to which they examine the wrapped object's annotations.
Often wrappers don't care about the value per se, only needing
specific information about the annotations. Even so, PEP 563
and the `if typing.TYPE_CHECKING` idiom can make it difficult
for wrappers to reliably determine the information they need at
runtime. This is an ongoing, chronic problem.
Under this PEP, wrappers will probably prefer `FORWARDREF` format
for their internal logic. But the wrapped objects need to support
all formats for their users.
### Documentation
PEP 563 stringized annotations were a boon for tools that
mechanically construct documentation.
Stringized type hints make for excellent documentation; type hints
as expressed in source code are often succinct and readable. However,
at runtime these same type hints can produce value at runtime whose repr
is a sprawling, nested, unreadable mess. Thus documentation users were
well-served by PEP 563 but poorly served with stock semantics.
Under this PEP, documentation users are expected to use `SOURCE` format.
## Motivation For This PEP
Python's original semantics for annotations made its use for
static type analysis painful due to forward reference problems.
PEP 563 solved the forward reference problem, and many
static type analysis users became happy early adopters of it.
But its unconventional solution created new problems for two
of the above cited use cases: runtime annotation users,
and wrappers.
First, stringized annotations didn't permit referencing local or
free variables, which meant many useful, reasonable approaches
to creating annotations were no longer viable. This was
particularly inconvenient for decorators that wrap existing
functions and classes, as these decorators often use closures.
Second, in order for `eval` to correctly look up globals in a
stringized annotation, you must first obtain a reference
to the correct module.
But class objects don't retain a reference to their globals.
PEP 563 suggests looking up a class's module by name in
`sys.modules`—a surprising requirement for a language-level
feature.
Additionally, complex but legitimate constructions can make it
difficult to determine the correct globals and locals dicts to
give to `eval` to properly evaluate a stringized annotation.
Even worse, in some situations it may simply be infeasible.
For example, some libraries (e.g. `typing.TypedDict`, `dataclasses`)
wrap a user class, then merge all the annotations from all that
class's base classes together into one cumulative annotations dict.
If those annotations were stringized, calling `eval` on them later
may not work properly, because the globals dictionary used for the
`eval` will be the module where the *user class* was defined,
which may not be the same module where the *annotation* was
defined. However, if the annotations were stringized because
of forward-reference problems, calling `eval` on them early
may not work either, due to the forward reference not being
resolvable yet. This has proved to be difficult to reconcile;
of the three bug reports linked to below, only one has been
marked as fixed.
* https://github.com/python/cpython/issues/89687
* https://github.com/python/cpython/issues/85421
* https://github.com/python/cpython/issues/90531
Even with proper globals *and* locals, `eval` can be unreliable
on stringized annotations.
`eval` can only succeed if all the symbols referenced in
an annotations are defined. If a stringized annotation refers
to a mixture of defined and undefined symbols, a simple `eval`
of that string will fail. This is a problem for libraries with
that need to examine the annotation, because they can't reliably
convert these stringized annotations into real values.
* Some libraries (e.g. `dataclasses`) solved this by foregoing real
values and performing lexical analysis of the stringized annotation,
which requires a lot of work to get right.
* Other libraries still suffer with this problem,
which can produce surprising runtime behavior.
https://github.com/python/cpython/issues/97727
Also, `eval()` is slow, and it isn't always available; it's
sometimes removed for space reasons on certain platforms.
`eval()` on MicroPython doesn't support the `locals`
argument, which makes converting stringized annotations
into real values at runtime even harder.
Finally, PEP 563 requires Python implementations to
stringize their annotations. This is surprising behavior—unprecedented
for a language-level feature, with a complicated implementation,
that must be updated whenever a new operator is added to the
language.
These problems motivated the research into finding a new
approach to solve the problems facing annotations users,
resulting in this PEP.
**************
Implementation
**************
## Observed semantics for annotations expressions
For any object `o` that supports annotations,
provided that all names evaluated in the annotations expressions
are bound before `o` is defined and never subsequently rebound,
`o.__annotations__` will produce an identical annotations dict both
when "stock" semantics are active and when this PEP is active.
In particular, name resolution will be performed identically in
both scenarios.
When this PEP is active, the value of `o.__annotations__`
won't be calculated until the first time `o.__annotations__`
itself is evaluated. All evaluation of the annotation expressions
is delayed until this moment, which also means that
* names referenced in the annotations expressions will use their
*current* value at this moment, and
* if evaluating the annotations expressions raises an exception,
that exception will be raised at this moment.
Once `o.__annotations__` is successfully calculated for the
first time, this value is cached and will be returned by future
requests for `o.__annotations__`.
## __annotate__ and __annotations__
Python supports annotations on three different types:
functions, classes, and modules. This PEP modifies
the semantics on all three of these types in a similar
way.
First, this PEP adds a new "dunder" attribute, `__annotate__`.
`__annotate__` must be a "data descriptor",
implementing all three actions: get, set, and delete.
The `__annotate__` attribute is always defined,
and may only be set to either `None` or to a callable.
(`__annotate__` cannot be deleted.) If an object
has no annotations, `__annotate__` should be
initialized to `None`, rather than to a function
that returns an empty dict.
The `__annotate__` data descriptor must have dedicated
storage inside the object to store the reference to its value.
The location of this storage at runtime is an implementation
detail. Even if it's visible to Python code, it should still
be considered an internal implementation detail, and Python
code should prefer to interact with it only via the
`__annotate__` attribute.
The callable stored in `__annotate__` must accept a
single required positional argument called `format`,
which will always be an `int` (or a subclass of `int`).
It must either return a dict (or subclass of dict) or
raise `NotImplementedError()`.
Here's a formal definition of `__annotate__`, as it will
appear in the "Magic methods" section of the Python
Language Reference:
`__annotate__(format: int) -> dict`
Returns a new dictionary object mapping attribute/parameter
names to their annotation values.
Takes a `format` parameter specifying the format in which
annotations values should be provided. Must be one of the
following:
`inspect.VALUE` (equivalent to the `int` constant `1`)
Values are the result of evaluating the annotation expressions.
`inspect.FORWARDREF` (equivalent to the `int` constant `2`)
Values are real annotation values (as per `inspect.VALUE` format)
for defined values, and `ForwardRef` proxies for undefined values.
Real objects may be exposed to, or contain references to,
`ForwardRef` proxy objects.
`inspect.SOURCE` (equivalent to the `int` constant `3`)
Values are the text string of the annotation as it
appears in the source code. May only be approximate;
whitespace may be normalized, and constant values may
be optimized. It's possible the exact values of these
strings could change in future version of Python.
If an `__annotate__` function doesn't support the requested
format, it must raise `NotImplementedError()`.
`__annotate__` functions must always support `1` (`inspect.VALUE`)
format; they must not raise `NotImplementedError()` when called with
`format=1`.
When called with `format=1`, an `__annotate__` function
may raise `NameError`; it must not raise `NameError` when called
requesting any other format.
If an object doesn't have any annotations, `__annotate__` should
preferably be set to `None` (it can't be deleted), rather than set to a
function that returns an empty dict.
When the Python compiler compiles an object with
annotations, it simultaneously compiles the appropriate
annotate function. This function, called with
the single positional argument `inspect.VALUE`,
computes and returns the annotations dict as defined
on that object. The Python compiler and runtime work
in concert to ensure that the function is bound to
the appropriate namespaces:
* For functions and classes, the globals dictionary will
be the module where the object was defined. If the object
is itself a module, its globals dictionary will be its
own dict.
* For methods on classes, and for classes, the locals dictionary
will be the class dictionary.
* If the annotations refer to free variables, the closure will
be the appropriate closure tuple containing cells for free variables.
Second, this PEP requires that the existing
`__annotations__` must be a "data descriptor",
implementing all three actions: get, set, and delete.
`__annotations__` must also have its own internal
storage it uses to cache a reference to the annotations dict:
* Class and module objects must
cache the annotations dict in their `__dict__`, using the key
`__annotations__`. This is required for backwards
compatibility reasons.
* For function objects, storage for the annotations dict
cache is an implementation detail. It's preferably internal
to the function object and not visible in Python.
This PEP defines semantics on how `__annotations__` and
`__annotate__` interact, for all three types that implement them.
In the following examples, `fn` represents a function, `cls`
represents a class, `mod` represents a module, and `o` represents
an object of any of these three types:
* When `o.__annotations__` is evaluated, and the internal storage
for `o.__annotations__` is unset, and `o.__annotate__` is set
to a callable, the getter for `o.__annotations__` calls
`o.__annotate__(1)`, then caches the result in its internal
storage and returns the result.
- To explicitly clarify one question that has come up multiple times:
this `o.__annotations__` cache is the *only* caching mechanism
defined in this PEP. There are *no other* caching mechanisms defined
in this PEP. The `__annotate__` functions generated by the Python
compiler explicitly don't cache any of the values they compute.
* Setting `o.__annotate__` to a callable invalidates the
cached annotations dict.
* Setting `o.__annotate__` to `None` has no effect on
the cached annotations dict.
* Deleting `o.__annotate__` raises `TypeError`.
`__annotate__` must always be set; this prevents unannotated
subclasses from inheriting the `__annotate__` method of one
of their base classes.
* Setting `o.__annotations__` to a legal value
automatically sets `o.__annotate__` to `None`.
* Setting `cls.__annotations__` or `mod.__annotations__`
to `None` otherwise works like any other attribute; the
attribute is set to `None`.
* Setting `fn.__annotations__` to `None` invalidates
the cached annotations dict. If `fn.__annotations__`
doesn't have a cached annotations value, and `fn.__annotate__`
is `None`, the `fn.__annotations__` data descriptor
creates, caches, and returns a new empty dict. (This is for
backwards compatibility with PEP 3107 semantics.)
## Changes to allowable annotations syntax
`__annotate__` now delays the evaluation of annotations until
`__annotations__` is referenced in the future. It also means
annotations are evaluated in a new function, rather than in the
original context where the object they were defined on was bound.
There are four operators with significant runtime side-effects
that were permitted in stock semantics, but are disallowed when
`from __future__ import annotations` is active, and will have
to be disallowed when this PEP is active:
* `:=`
* `yield`
* `yield from`
* `await`
## Changes to `inspect.get_annotations` and `typing.get_type_hints`
(This PEP makes frequent reference to these two functions. In the future
it will refer to them collectively as "the helper functions", as they help
user code work with annotations.)
These two functions extract and return the annotations from an object.
`inspect.get_annotations` returns the annotations unchanged;
for the convenience of static typing users, `typing.get_type_hints`
makes some modifications to the annotations before it returns them.
The canonical annotation lookup helper is `annotationlib.get_annotations`,
which is also exposed through `inspect.get_annotations`.
This PEP adds a new keyword-only parameter to these two functions,
`format`. `format` specifies what format the values in the
annotations dict should be returned in.
The `format` parameter on these two functions accepts the same values
as the `format` parameter on the `__annotate__` magic method
defined above; however, these `format` parameters also have a default
value of `inspect.VALUE`.
When either `__annotations__` or `__annotate__` is updated on an
object, the other of those two attributes is now out-of-date and should also
either be updated or deleted (set to `None`, in the case of `__annotate__`
which cannot be deleted). In general, the semantics established in the previous
section ensure that this happens automatically. However, there's one case which
for all practical purposes can't be handled automatically: when the dict cached
by `o.__annotations__` is itself modified, or when mutable values inside that
dict are modified.
Since this can't be handled in code, it must be handled in
documentation. This PEP proposes amending the documentation
for `inspect.get_annotations` (and similarly for
`typing.get_type_hints`) as follows:
If you directly modify the `__annotations__` dict on an object,
by default these changes may not be reflected in the dictionary
returned by `inspect.get_annotations` when requesting either
`SOURCE` or `FORWARDREF` format on that object. Rather than
modifying the `__annotations__` dict directly, consider replacing
that object's `__annotate__` method with a function computing
the annotations dict with your desired values. Failing that, it's
best to overwrite the object's `__annotate__` method with `None`
to prevent `inspect.get_annotations` from generating stale results
for `SOURCE` and `FORWARDREF` formats.
## The `stringizer` and the `fake globals` environment
As originally proposed, this PEP supported many runtime
annotation user use cases, and many static type user use cases.
But this was insufficient--this PEP could not be accepted
until it satisfied *all* extant use cases. This became
a longtime blocker of this PEP until Carl Meyer proposed
the "stringizer" and the "fake globals" environment as
described below. These techniques allow this PEP to support
both the `FORWARDREF` and `SOURCE` formats, ably
satisfying all remaining uses cases.
In a nutshell, this technique involves running a
Python-compiler-generated `__annotate__` function in
an exotic runtime environment. Its normal `globals`
dict is replaced with what's called a "fake globals" dict.
A "fake globals" dict is a dict with one important difference:
every time you "get" a key from it that isn't mapped,
it creates, caches, and returns a new value for that key
(as per the `__missing__` callback for a dictionary).
That value is a an instance of a novel type referred to
as a "stringizer".
A "stringizer" is a Python class with highly unusual behavior.
Every stringizer is initialized with its "value", initially
the name of the missing key in the "fake globals" dict. The
stringizer then implements every Python "dunder" method used to
implement operators, and the value returned by that method
is a new stringizer whose value is a text representation
of that operation.
When these stringizers are used in expressions, the result
of the expression is a new stringizer whose name textually
represents that expression. For example, let's say
you have a variable `f`, which is a reference to a
stringizer initialized with the value `'f'`. Here are
some examples of operations you could perform on `f` and
the values they would return:
```
>>> f
Stringizer('f')
>>> f + 3
Stringizer('f + 3')
>> f["key"]
Stringizer('f["key"]')
```
Bringing it all together: if we run a Python-generated
`__annotate__` function, but we replace its globals
with a "fake globals" dict, all undefined symbols it
references will be replaced with stringizer proxy objects
representing those symbols, and any operations performed
on those proxies will in turn result in proxies
representing that expression. This allows `__annotate__`
to complete, and to return an annotations dict, with
stringizer instances standing in for names and entire
expressions that could not have otherwise been evaluated.
In practice, the "stringizer" functionality will be implemented
in the `ForwardRef` object currently defined in the
`typing` module. `ForwardRef` will be extended to
implement all stringizer functionality; it will also be
extended to support evaluating the string it contains,
to produce the real value (assuming all symbols referenced
are defined). This means the `ForwardRef` object
will retain references to the appropriate "globals",
"locals", and even "closure" information needed to
evaluate the expression.
This technique is the core of how `inspect.get_annotations`
supports `FORWARDREF` and `SOURCE` formats. Initially,
`inspect.get_annotations` will call the object's
`__annotate__` method requesting the desired format.
If that raises `NotImplementedError`, `inspect.get_annotations`
will construct a "fake globals" environment, then call
the object's `__annotate__` method.
The public helper for invoking an `__annotate__` function for a requested format
is `annotationlib.call_annotate_function`.
* `inspect.get_annotations` produces `SOURCE` format
by creating a new empty "fake globals" dict, binding it
to the object's `__annotate__` method, calling that
requesting `VALUE` format, and then extracting the string
"value" from each `ForwardRef` object
in the resulting dict.
* `inspect.get_annotations` produces `FORWARDREF` format
by creating a new empty "fake globals" dict, pre-populating
it with the current contents of the `__annotate__` method's
globals dict, binding the "fake globals" dict to the object's
`__annotate__` method, calling that requesting `VALUE`
format, and returning the result.
This entire technique works because the `__annotate__` functions
generated by the compiler are controlled by Python itself, and
are simple and predictable. They're
effectively a single `return` statement, computing and
returning the annotations dict. Since most operations needed
to compute an annotation are implemented in Python using dunder
methods, and the stringizer supports all the relevant dunder
methods, this approach is a reliable, practical solution.
However, it's not reasonable to attempt this technique with
just any `__annotate__` method. This PEP assumes that
third-party libraries may implement their own `__annotate__`
methods, and those functions would almost certainly work
incorrectly when run in this "fake globals" environment.
For that reason, this PEP allocates a flag on code objects,
one of the unused bits in `co_flags`, to mean "This code
object can be run in a 'fake globals' environment." This
makes the "fake globals" environment strictly opt-in, and
it's expected that only `__annotate__` methods generated
by the Python compiler will set it.
The weakness in this technique is in handling operators which
don't directly map to dunder methods on an object. These are
all operators that implement some manner of flow control,
either branching or iteration:
* Short-circuiting `or`
* Short-circuiting `and`
* Ternary operator (the `if` / `then` operator)
* Generator expressions
* List / dict / set comprehensions
* Iterable unpacking
As a rule these techniques aren't used in annotations,
so it doesn't pose a problem in practice. However, the
recent addition of `TypeVarTuple` to Python does use
iterable unpacking. The dunder methods
involved (`__iter__` and `__next__`) don't permit
distinguishing between iteration use cases; in order to
correctly detect which use case was involved, mere
"fake globals" and a "stringizer" wouldn't be sufficient;
this would require a custom bytecode interpreter designed
specifically around producing `SOURCE` and `FORWARDREF`
formats.
Thankfully there's a shortcut that will work fine:
the stringizer will simply assume that when its
iteration dunder methods are called, it's in service
of iterator unpacking being performed by `TypeVarTuple`.
It will hard-code this behavior. This means no other
technique using iteration will work, but in practice
this won't inconvenience real-world use cases.
Finally, note that the "fake globals" environment
will also require constructing a matching "fake locals"
dictionary, which for `FORWARDREF` format will be
pre-populated with the relevant locals dict. The
"fake globals" environment will also have to create
a fake "closure", a tuple of `ForwardRef` objects
pre-created with the names of the free variables
referenced by the `__annotate__` method.
`ForwardRef` proxies created from `__annotate__`
methods that reference free variables will map the
names and closure values of those free variables into
the locals dictionary, to ensure that `eval` uses
the correct values for those names.
## Compiler-generated `__annotate__` functions
As mentioned in the previous section, the `__annotate__`
functions generated by the compiler are simple. They're
mainly a single `return` statement, computing and
returning the annotations dict.
However, the protocol for `inspect.get_annotations`
to request either `FORWARDREF` or `SOURCE` format
requires first asking the `__annotate__` method to
produce it. `__annotate__` methods generated by
the Python compiler won't support either of these
formats and will raise `NotImplementedError()`.
## Third-party `__annotate__` functions
Third-party classes and functions will likely need
to implement their own `__annotate__` methods,
so that downstream users of
those objects can take full advantage of annotations.
In particular, wrappers will likely need to transform
the annotation dicts produced by the wrapped object: adding,
removing, or modifying the dictionary in some way.
Most of the time, third-party code will implement
their `__annotate__` methods by calling
`inspect.get_annotations` on some existing upstream
object. For example, wrappers will likely request the
annotations dict for their wrapped object,
in the format that was requested from them, then
modify the returned annotations dict as appropriate
and return that. This allows third-party code to
leverage the "fake globals" technique without
having to understand or participate in it.
Third-party libraries that support both pre- and
post-PEP-649 versions of Python will have to innovate
their own best practices on how to support both.
One sensible approach would be for their wrapper to
always support `__annotate__`, then call it requesting
`VALUE` format and store the result as the
`__annotations__` on their wrapper object.
This would support pre-649 Python semantics, and be
forward-compatible with post-649 semantics.
## Pseudocode
Here's high-level pseudocode for `inspect.get_annotations`:
```
def get_annotations(o, format):
if format == VALUE:
return dict(o.__annotations__)
if format == FORWARDREF:
try:
return dict(o.__annotations__)
except NameError:
pass
if not hasattr(o.__annotate__):
return {}
c_a = o.__annotate__
try:
return c_a(format)
except NotImplementedError:
if not can_be_called_with_fake_globals(c_a):
return {}
c_a_with_fake_globals = make_fake_globals_version(c_a, format)
return c_a_with_fake_globals(VALUE)
```
Here's what a Python compiler-generated `__annotate__` method
might look like if it was written in Python:
```
def __annotate__(self, format):
if format != 1:
raise NotImplementedError()
return { ... }
```
Here's how a third-party wrapper class might implement
`__annotate__`. In this example, the wrapper works
like `functools.partial`, pre-binding one parameter of
the wrapped callable, which for simplicity must be named
`arg`:
```
def __annotate__(self, format):
ann = inspect.get_annotations(self.wrapped_fn, format)
if 'arg' in ann:
del ann['arg']
return ann
```
## Other modifications to the Python runtime
This PEP does not dictate exactly how it should be
implemented; that is left up to the language implementation
maintainers. However, the best implementation of this
PEP may require adding additional information to existing
Python objects, which is implicitly condoned by the acceptance
of this PEP.
For example, it may be necessary to add a
`__globals__` attribute to class objects, so that the
`__annotate__` function for that class can be lazily
bound, only on demand. Also, `__annotate__` functions
defined on methods defined in a class may need to retain
a reference to the class's `__dict__`, in order to
correctly evaluate names bound in that class. It's expected
that the CPython implementation of this PEP will include
both those new attributes.
All such new information added to existing Python objects
should be done with "dunder" attributes, as they will of
course be implementation details.
## Interactive REPL Shell
The semantics established in this PEP also hold true when executing
code in Python's interactive REPL shell, except for module annotations
in the interactive module (`__main__`) itself. Since that module is
never "finished", there's no specific point where we can compile the
`__annotate__` function.
For the sake of simplicity, in this case we forego delayed evaluation.
Module-level annotations in the REPL shell will continue to work
exactly as they do with "stock semantics", evaluating immediately and
setting the result directly inside the `__annotations__` dict.
## Annotations On Local Variables Inside Functions
Python supports syntax for local variable annotations inside
functions. However, these annotations have no runtime
effect--they're discarded at compile-time. Therefore, this
PEP doesn't need to do anything to support them, the same
as stock semantics and PEP 563.
## Prototype
The original prototype implementation of this PEP can be found here:
https://github.com/larryhastings/co_annotations/
As of this writing, the implementation is severely out of date;
it's based on Python 3.10 and implements the semantics of the
first draft of this PEP, from early 2021. It will be updated
shortly.
## Performance Comparison
Performance with this PEP is generally favorable. There are four
scenarios to consider:
* the runtime cost when annotations aren't defined,
* the runtime cost when annotations are defined but *not* referenced, and
* the runtime cost when annotations are defined and referenced as objects.
* the runtime cost when annotations are defined and referenced as strings.
We'll examine each of these scenarios in the context of all three
semantics for annotations: stock, PEP 563, and this PEP.
When there are no annotations, all three semantics have the same
runtime cost: zero. No annotations dict is created and no code is
generated for it. This requires no runtime processor time and
consumes no memory.
When annotations are defined but not referenced, the runtime cost
of Python with this PEP is roughly the same as PEP 563, and
improved over stock. The specifics depend on the object
being annotated:
* With stock semantics, the annotations dict is always built, and
set as an attribute of the object being annotated.
* In PEP 563 semantics, for function objects, a precompiled
constant (a specially constructed tuple) is set as an attribute
of the function. For class and module objects, the annotations
dict is always built and set as an attribute of the class or module.
* With this PEP, a single object is set as an attribute of the
object being annotated. Most of the time, this object is
a constant (a code object), but when the annotations require a
class namespace or closure, this object will be a tuple constructed
at binding time.
When annotations are both defined and referenced as objects, code using
this PEP should be much faster than PEP 563, and be as fast
or faster than stock. PEP 563 semantics requires invoking
`eval()` for every value inside an annotations dict which is
enormously slow. And the implementation of this PEP generates measurably
more efficient bytecode for class and module annotations than stock
semantics; for function annotations, this PEP and stock semantics
should be about the same speed.
The one case where this PEP will be noticeably slower than PEP 563 is when
annotations are requested as strings; it's hard to beat "they are already
strings." But stringified annotations are intended for online documentation use
cases, where performance is less likely to be a key factor.
Memory use should also be comparable in all three scenarios across
all three semantic contexts. In the first and third scenarios,
memory usage should be roughly equivalent in all cases.
In the second scenario, when annotations are defined but not
referenced, using this PEP's semantics will mean the
function/class/module will store one unused code object (possibly
bound to an unused function object); with the other two semantics,
they'll store one unused dictionary or constant tuple.
***********************
Backwards Compatibility
***********************
## Backwards Compatibility With Stock Semantics
This PEP preserves nearly all existing behavior of
annotations from stock semantics:
* The format of the annotations dict stored in
the `__annotations__` attribute is unchanged.
Annotations dicts contain real values, not strings
as per PEP 563.
* Annotations dicts are mutable, and any changes to them are
preserved.
* The `__annotations__` attribute can be explicitly set,
and any legal value set this way will be preserved.
* The `__annotations__` attribute can be deleted using
the `del` statement.
Most code that works with stock semantics should
continue to work when this PEP is active without any
modification necessary. But there are exceptions,
as follows.
First, there's a well-known idiom for accessing class
annotations which may not work correctly when this
PEP is active. The original implementation of class
annotations had what can only be called a bug: if a class
didn't define any annotations of its own, but one
of its base classes did define annotations, the class
would "inherit" those annotations. This behavior
was never desirable, so user code found a workaround:
instead of accessing the annotations on the class
directly via `cls.__annotations__`, code would
access the class's annotations via its dict as in
`cls.__dict__.get("__annotations__", {})`. This
idiom worked because classes stored their annotations
in their `__dict__`, and accessing them this way
avoided the lookups in the base classes. The technique
relied on implementation details of CPython, so it
was never supported behavior--though it was necessary.
However, when this PEP is active, a class may have
annotations defined but hasn't yet called `__annotate__`
and cached the result, in which case this approach
would lead to mistakenly assuming the class didn't have
annotations.
In any case, the bug was fixed as of Python 3.10, and the
idiom should no longer be used. Also as of Python 3.10,
there's an
[Annotations HOWTO](https://docs.python.org/3/howto/annotations.html)
that defines best practices
for working with annotations; code that follows these
guidelines will work correctly even when this PEP is
active, because it suggests using different approaches
to get annotations from class objects based on the
Python version the code runs under.
Since delaying the evaluation of annotations until they are
introspected changes the semantics of the language, it's observable
from within the language. Therefore it's *possible* to write code
that behaves differently based on whether annotations are
evaluated at binding time or at access time, e.g.
```
mytype = str
def foo(a:mytype): pass
mytype = int
print(foo.__annotations__['a'])
```
This will print `<class 'str'>` with stock semantics
and `<class 'int'>` when this PEP is active. This is
therefore a backwards-incompatible change. However, this
example is poor programming style, so this change seems
acceptable.
There are two uncommon interactions possible with class
and module annotations that work with stock semantics
that would no longer work when this PEP was active.
These two interactions would have to be prohibited. The
good news is, neither is common, and neither is considered
good practice. In fact, they're rarely seen outside of
Python's own regression test suite. They are:
* *Code that sets annotations on module or class attributes
from inside any kind of flow control statement.* It's
currently possible to set module and class attributes with
annotations inside an `if` or `try` statement, and it works
as one would expect. It's untenable to support this behavior
when this PEP is active.
* *Code in module or class scope that references or modifies the
local* `__annotations__` *dict directly.* Currently, when
setting annotations on module or class attributes, the generated
code simply creates a local `__annotations__` dict, then adds
mappings to it as needed. It's possible for user code
to directly modify this dict, though this doesn't seem to be
an intentional feature. Although it would be possible to support
this after a fashion once this PEP was active, the semantics
would likely be surprising and wouldn't make anyone happy.
Note that these are both also pain points for static type checkers,
and are unsupported by those tools. It seems reasonable to
declare that both are at the very least unsupported, and their
use results in undefined behavior. It might be worth making a
small effort to explicitly prohibit them with compile-time checks.
Finally, if this PEP is active, annotation values shouldn't use
the `if / else` ternary operator. Although this will work
correctly when accessing `o.__annotations__` or requesting
`inspect.VALUE` from a helper function, the boolean expression
may not compute correctly with `inspect.FORWARDREF` when
some names are defined, and would be far less correct with
`inspect.SOURCE`.
## Backwards Compatibility With PEP 563 Semantics
PEP 563 changed the semantics of annotations. When its semantics
are active, annotations must assume they will be evaluated in
*module-level* or *class-level* scope. They may no longer refer directly
to local variables in the current function or an enclosing function.
This PEP removes that restriction, and annotations may refer any
local variable.
PEP 563 requires using `eval` (or a helper function like
`typing.get_type_hints` or `inspect.get_annotations` that
uses `eval` for you) to convert stringized annotations into
their "real" values. Existing code that activates stringized
annotations, and calls `eval()` directly to convert the strings
back into real values, can simply remove the `eval()` call.
Existing code using a helper function would continue to work
unchanged, though use of those functions may become optional.
Static typing users often have modules that only contain
inert type hint definitions--but no live code. These modules
are only needed when running static type checking; they aren't
used at runtime. But under stock semantics, these modules
have to be imported in order for the runtime to evaluate and
compute the annotations. Meanwhile, these modules often
caused circular import problems that could be difficult or
even impossible to solve. PEP 563 allowed users to solve
these circular import problems by doing two things. First,
they activated PEP 563 in their modules, which meant annotations
were constant strings, and didn't require the real symbols to
be defined in order for the annotations to be computable.
Second, this permitted users to only import the problematic
modules in an `if typing.TYPE_CHECKING` block. This allowed
the static type checkers to import the modules and the type
definitions inside, but they wouldn't be imported at runtime.
So far, this approach will work unchanged when this PEP is
active; `if typing.TYPE_CHECKING` is supported behavior.
However, some codebases actually *did* examine their
annotations at runtime, even when using the `if typing.TYPE_CHECKING`
technique and not importing definitions used in their annotations.
These codebases examined the annotation strings *without
evaluating them,* instead relying on identity checks or
simple lexical analysis on the strings.
This PEP supports these techniques too. But users will need
to port their code to it. First, user code will need to use
`inspect.get_annotations` or `typing.get_type_hints` to
access the annotations; they won't be able to simply get the
`__annotations__` attribute from their object. Second,
they will need to specify either `inspect.FORWARDREF`
or `inspect.SOURCE` for the `format` when calling that
function. This means the helper function can succeed in
producing the annotations dict, even when not all the symbols
are defined. Code expecting stringized annotations should
work unmodified with `inspect.SOURCE` formatted annotations
dicts; however, users should consider switching to
`inspect.FORWARDREF`, as it may make their analysis easier.
Similarly, PEP 563 permitted use of class decorators on
annotated classes in a way that hadn't previously been possible.
Some class decorators (e.g. `dataclasses`) examine the annotations
on the class. Because class decorators using the `@` decorator
syntax are run before the class name is bound, they can cause
unsolvable circular-definition problems. If you annotate attributes
of a class with references to the class itself, or annotate attributes
in multiple classes with circular references to each other, you
can't decorate those classes with the `@` decorator syntax
using decorators that examine the annotations. PEP 563 allowed
this to work, as long as the decorators examined the strings lexically
and didn't use `eval` to evaluate them (or handled the `NameError`
with further workarounds). When this PEP is active, decorators will
be able to compute the annotations dict in `inspect.SOURCE` or
`inspect.FORWARDREF` format using the helper functions. This
will permit them to analyze annotations containing undefined
symbols, in the format they prefer.
Early adopters of PEP 563 discovered that "stringized"
annotations were useful for automatically-generated documentation.
Users experimented with this use case, and Python's `pydoc`
has expressed some interest in this technique. This PEP supports
this use case; the code generating the documentation will have to be
updated to use a helper function to access the annotations in
`inspect.SOURCE` format.
Finally, the warnings about using the `if / else` ternary
operator in annotations apply equally to users of PEP 563.
It currently works for them, but could produce incorrect
results when requesting some formats from the helper functions.
If this PEP is accepted, PEP 563 will be deprecated and
eventually removed. To facilitate this transition for early
adopters of PEP 563, who now depend on its semantics,
`inspect.get_annotations` and `typing.get_type_hints` will
implement a special affordance.
The Python compiler won't generate annotation code objects
for objects defined in a module where PEP 563 semantics are
active, even if this PEP is accepted. So, under normal
circumstances, requesting `inspect.SOURCE` format from a
helper function would return an empty dict. As an affordance,
to facilitate the transition, if the helper functions detect
that an object was defined in a module with PEP 563 active,
and the user requests `inspect.SOURCE` format, they'll return
the current value of the `__annotations__` dict, which in
this case will be the stringized annotations. This will allow
PEP 563 users who lexically analyze stringized annotations
to immediately change over to requesting `inspect.SOURCE` format
from the helper functions, which will hopefully smooth their
transition away from PEP 563.
**************
Rejected Ideas
**************
## "Just store the strings"
One proposed idea for supporting `SOURCE` format was for
the Python compiler to emit the actual source code for the
annotation values somewhere, and to furnish that when
the user requested `SOURCE` format.
This idea wasn't rejected so much as categorized as
"not yet". We already know we need to support `FORWARDREF`
format, and that technique can be adapted to support
`SOURCE` format in just a few lines. There are many
unanswered questions about this approach:
* Where would we store the strings? Would they always
be loaded when the annotated object was created, or
would they be lazy-loaded on demand? If so, how
would the lazy-loading work?
* Would the "source code" include the newlines and
comments of the original? Would it preserve all
whitespace, including indents and extra spaces used
purely for formatting?
It's possible we'll revisit this topic in the future,
if improving the fidelity of `SOURCE` values to the
original source code is judged sufficiently important.
****************
Acknowledgements
****************
Thanks to Carl Meyer, Barry Warsaw, Eric V. Smith,
Mark Shannon, Jelle Zijlstra, and Guido van Rossum for ongoing
feedback and encouragement.
Particular thanks to several individuals who contributed key ideas
that became some of the best aspects of this proposal:
* Carl Meyer suggested the "stringizer" technique that made
`FORWARDREF` and `SOURCE` formats possible, which
allowed making forward progress on this PEP possible after
a year of languishing due to seemingly-unfixable problems.
He also suggested the affordance for PEP 563 users where
`inspect.SOURCE` will return the stringized annotations,
and many more suggestions besides. Carl was also the primary
correspondent in private email threads discussing this PEP,
and was a tireless resource and voice of sanity. This PEP
would almost certainly not have been accepted it were it not
for Carl's contributions.
* Mark Shannon suggested building the entire annotations dict
inside a single code object, and only binding it to a function
on demand.
* Guido van Rossum suggested that `__annotate__`
functions should duplicate the name visibility rules of
annotations under "stock" semantics.
* Jelle Zijlstra contributed not only feedback--but code!
**********
References
**********
* https://github.com/larryhastings/co_annotations/issues
* https://discuss.python.org/t/two-polls-on-how-to-revise-pep-649/23628
* https://discuss.python.org/t/a-massive-pep-649-update-with-some-major-course-corrections/25672
*********
Copyright
*********
This document is placed in the public domain or under the
CC0-1.0-Universal license, whichever is more permissive.
## Linked Issue #89687 — Cross-module dataclass inheritance breaks get_type_hints
This CPython issue (migrated from BPO 45524, opened 2021-10-19) is cited in the *Motivation For This PEP* section as one of the three bug reports illustrating the difficulty of selecting the correct `globals`/`locals` dicts to pass to `eval` when resolving stringized annotations.
The bug describes that `typing.get_type_hints()` fails to resolve annotations on dataclasses that inherit fields from a base class defined in a different module: when PEP 563 stringized annotations are active, the `globals` dict used by `eval` is the module where the *subclass* lives, not the module where the inherited annotation was originally written. Names defined only in the base class's module cannot be resolved. The issue type is `type-bug`/`library`, affecting Python 3.9–3.11. Linked CPython PR: [#29158](https://github.com/python/cpython/pull/29158). As of the PEP's authoring, the issue remained open.
*Source: https://github.com/python/cpython/issues/89687*
## Linked Issue #85421 — TypedDict inheritance doesn't work with get_type_hints and postponed evaluation of annotations across modules
This CPython issue (migrated from BPO 41249, opened 2020-07-08, closed 2021-07-17 as `fixed`) is cited in the *Motivation For This PEP* section as one of the three bug reports illustrating the difficulty of selecting the correct `globals`/`locals` dicts to pass to `eval` for stringized annotations. The PEP explicitly notes that, of the three linked bug reports, "only one has been marked as fixed" — this is the one.
The bug describes that `TypedDict` subclasses fail to resolve cross-module forward references when `from __future__ import annotations` is active. `typing.get_type_hints()` looks up the `TypedDict`'s globals using the wrong module's namespace, so annotations referring to names defined in the parent `TypedDict`'s module cannot be resolved. The issue type is `type-bug`/`library`, affecting Python 3.7–3.11. Linked CPython PRs: [#27017](https://github.com/python/cpython/pull/27017), [#27204](https://github.com/python/cpython/pull/27204), [#27205](https://github.com/python/cpython/pull/27205).
*Source: https://github.com/python/cpython/issues/85421*
## Linked Issue #90531 — TypedDict and NamedTuple do not evaluate cross-module ForwardRef in all cases
This CPython issue (migrated from BPO 46373, opened 2022-01-14) is cited in the *Motivation For This PEP* section as the third of the three bug reports illustrating the eval/globals problem with stringized annotations.
The bug describes that `TypedDict` and `NamedTuple` fail to evaluate `ForwardRef` objects in all cases when annotations come from a different module than the one defining the `TypedDict`/`NamedTuple`. Cross-module forward references can remain unresolved in the dict returned by `typing.get_type_hints()`. The issue type is `type-bug`/`library`, affecting Python 3.10–3.11. As of the PEP's authoring, the issue remained open.
*Source: https://github.com/python/cpython/issues/90531*
## Linked Issue #97727 — `__future__` annotations breaks `TypedDict` `__required/optional_keys__`
This CPython issue is cited in the *Motivation For This PEP* section as evidence that libraries which still attempt to `eval` stringized annotations "can produce surprising runtime behavior" when names are partially undefined.
The bug report's body:
> # Bug report
>
> `from __future__ import annotations` appears to break `TypedDict` required/optional keys ending up in `__required_keys__` and `__optional_keys__`. mypy works as expected though.
>
> Using the example from https://peps.python.org/pep-0655/#usage-in-python-3-11 as the base
>
> ```shellsession
> $ cat t.py
> from __future__ import annotations
>
> from typing_extensions import NotRequired, TypedDict
>
> class Dog(TypedDict):
> name: str
> owner: NotRequired[str]
>
> print("required", Dog.__required_keys__)
> print("optional", Dog.__optional_keys__)
> $ python3 t.py
> required frozenset({'name', 'owner'})
> optional frozenset()
> ```
>
> With the `__future__` import removed, works as expected:
>
> ```shellsession
> $ cat t.py
> from typing_extensions import NotRequired, TypedDict
>
> class Dog(TypedDict):
> name: str
> owner: NotRequired[str]
>
> print("required", Dog.__required_keys__)
> print("optional", Dog.__optional_keys__)
> $ python3 t.py
> required frozenset({'name'})
> optional frozenset({'owner'})
> ```
>
> Note: breaks across different variations of `total` and `Required`/`NotRequired` and `typing_extensions` vs `typing` imports, above is just one example.
>
> https://peps.python.org/pep-0655/#how-to-teach-this contains an example with the `__future__` annotations import in place with no mention that it would not cause `__required_keys__` and `__optional_keys__` becoming populated as expected, so I'm assuming this is a bug.
>
> # Your environment
>
> - CPython versions tested on:
> - 3.9.7 + `NotRequired` and `TypedDict` imports from `typing_extensions`
> - 3.10.7 + above mentioned imports from `typing_extensions`
> - current 3.11.0rc2+ + above mentioned imports from `typing_extensions`
> - current 3.11.0rc2+ above mentioned imports from `typing`
> - Operating system and architecture: Linux x86_64
The bug is that, under stringized annotations, the `Required`/`NotRequired` markers cannot be detected lexically when their qualified form (e.g., `typing_extensions.NotRequired[...]`) appears as a string — leading `TypedDict.__required_keys__`/`__optional_keys__` to be populated incorrectly.
*Source: https://github.com/python/cpython/issues/97727*
|