@@ -30,9 +30,11 @@ specification:
30
30
that can be run in a "fake globals" environment. Instead, we add a fourth format,
31
31
``VALUE_WITH_FAKE_GLOBALS ``, to allow third-party implementors of annotate functions to
32
32
indicate what formats they support.
33
- * Setting the ``__annotations__ `` attribute directly will not affect the ``__annotate__ `` attribute .
33
+ * Deleting the ``__annotations__ `` attribute directly will also clear ``__annotate__ ``.
34
34
* We add functionality to allow evaluating type alias values and type parameter bounds and defaults
35
35
(which were added by :pep: `695 ` and :pep: `696 `) using PEP 649-like semantics.
36
+ * The ``SOURCE `` format is renamed to ``STRING `` to improve clarity and reduce the risk of
37
+ user confusion.
36
38
37
39
Motivation
38
40
==========
@@ -229,13 +231,13 @@ The module will contain the following functionality:
229
231
dictionary. This is intended to be used for evaluating deferred attributes introduced by
230
232
:pep: `695 ` and :pep: `696 `; see below for details. *func * may be ``None ``
231
233
for convenience; if ``None `` is passed, the function also returns ``None ``.
232
- * ``annotations_to_source (annotations: dict[str, object]) -> dict[str, str] ``: a function that
234
+ * ``annotations_to_string (annotations: dict[str, object]) -> dict[str, str] ``: a function that
233
235
converts each value in an annotations dictionary to a string representation.
234
236
This is useful for
235
237
implementing the ``SOURCE `` format in cases where the original source is not available,
236
238
such as in the functional syntax for :py:class: `typing.TypedDict `.
237
- * ``value_to_source (value: object) -> str ``: a function that converts a single value to a
238
- string representation. This is used by ``annotations_to_source ``.
239
+ * ``value_to_string (value: object) -> str ``: a function that converts a single value to a
240
+ string representation. This is used by ``annotations_to_string ``.
239
241
It uses ``repr() `` for most values, but for types it returns the fully qualified name.
240
242
It is also useful as a helper for the ``repr() `` of a number of objects in the
241
243
:py:mod: `typing ` and :py:mod: `collections.abc ` modules.
@@ -629,29 +631,23 @@ Third-party code that implements ``__annotate__`` functions should raise
629
631
and the function is not prepared to be run in a "fake globals" environment.
630
632
This should be mentioned in the data model documentation for ``__annotate__ ``.
631
633
632
- Effect of setting ``__annotations__ ``
633
- =====================================
634
+ Effect of deleting ``__annotations__ ``
635
+ ======================================
634
636
635
637
:pep: `649 ` specifies:
636
638
637
639
Setting ``o.__annotations__ `` to a legal value
638
640
automatically sets ``o.__annotate__ `` to ``None ``.
639
641
640
- We would prefer to keep ``__annotate__ `` unchanged when ``__annotations__ ``
641
- is written to. Conceptually, ``__annotate__ `` provides the ground truth
642
- and ``__annotations__ `` is merely a cache, and we shouldn't throw away the
643
- ground truth if the cache is modified.
644
-
645
- The motivation for :pep: `649 `'s behavior is to keep the two attributes in sync.
646
- However, this is impossible in general; if the ``__annotations__ `` dictionary
647
- is modified in place, this will not be reflected in the ``__annotate__ `` attribute.
648
- The overall mental model for this area will be simpler if setting ``__annotations__ ``
649
- has no effect on ``__annotate__ ``.
642
+ However, the PEP does not say what happens if the ``__annotations__ `` attribute
643
+ is deleted (using ``del ``). It seems most consistent that deleting the attribute
644
+ will also delete ``__annotate__ ``.
650
645
651
646
Specification
652
647
-------------
653
648
654
- The value of ``__annotate__ `` is not changed when ``__annotations__ `` is set.
649
+ Deleting the ``__annotations__ `` attribute on functions, modules, and classes
650
+ results in setting ``__annotate__ `` to None.
655
651
656
652
Deferred evaluation of PEP 695 and 696 objects
657
653
==============================================
@@ -730,6 +726,34 @@ can use the ``ForwardRef.evaluate`` method.
730
726
If use cases come up in the future, we could add additional functionality,
731
727
such as a new method that re-evaluates the annotation from scratch.
732
728
729
+ Renaming ``SOURCE `` to ``STRING ``
730
+ =================================
731
+
732
+ The ``SOURCE `` format is meant for tools that need to show a human-readable
733
+ format that is close to the original source code. However, we cannot retrieve
734
+ the original source in ``__annotate__ `` functions, and in some cases, we have
735
+ ``__annotate__ `` functions in Python code that do not have access to the original
736
+ code. For example, this applies to :py:func: `dataclasses.make_dataclass `
737
+ and the call-based syntax for :py:class: `typing.TypedDict `.
738
+
739
+ This makes the name ``SOURCE `` a bit of a misnomer. The goal of the format
740
+ should indeed be to recreate the source, but the name is likely to mislead
741
+ users in practice. A more neutral name would emphasize that the format returns
742
+ an annotation dictionary with only strings. We suggest ``STRING ``.
743
+
744
+ Specification
745
+ -------------
746
+
747
+ The ``SOURCE `` format is renamed to ``STRING ``. To reiterate the changes in this
748
+ PEP, the four supported formats are now:
749
+
750
+ - ``VALUE ``: the default format, which evaluates the annotations and returns the
751
+ resulting values.
752
+ - ``VALUE_WITH_FAKE_GLOBALS ``: for internal use; should be handled like ``VALUE ``
753
+ by annotate functions that support execution with fake globals.
754
+ - ``FORWARDREF ``: replaces undefined names with ``ForwardRef `` objects.
755
+ - ``STRING ``: returns strings, attempts to recreate code close to the original source.
756
+
733
757
Miscellaneous implementation details
734
758
====================================
735
759
0 commit comments