Skip to content

Commit

Permalink
Rename; set value to 2 as suggested by Larry
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 25, 2024
1 parent 750b9e5 commit 390207b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions peps/pep-0749.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ specification:
and code that uses :py:func:`functools.wraps`.
* There will not be a code flag for marking ``__annotate__`` functions
that can be run in a "fake globals" environment. Instead, we add a fourth format,
``FAKE_GLOBALS_VALUE``, to allow third-party implementors of annotate functions to
``VALUE_WITH_FAKE_GLOBALS``, to allow third-party implementors of annotate functions to
indicate what formats they support.
* Setting the ``__annotations__`` attribute directly will not affect the ``__annotate__`` attribute.
* We add functionality to allow evaluating type alias values and type parameter bounds and defaults
Expand Down Expand Up @@ -202,7 +202,7 @@ The module will contain the following functionality:
replace the ``VALUE``, ``FORWARDREF``, and ``SOURCE`` formats in :pep:`649`.
PEP 649 proposed to make these values global members of the :py:mod:`inspect`
module; we prefer to place them within an enum. We propose to add a fourth format,
``FAKE_GLOBALS_VALUE`` (see below).
``VALUE_WITH_FAKE_GLOBALS`` (see below).
* ``ForwardRef``: a class representing a forward reference; it may be returned by
``get_annotations()`` when the format is ``FORWARDREF``. The existing class
:py:class:`typing.ForwardRef` will become an alias of this class. Its members include:
Expand Down Expand Up @@ -562,8 +562,8 @@ This approach would also mean that accessing ``.__annotations__`` on an instance
of an annotated class no longer works. While this behavior is not documented,
it is a long-standing feature of Python and is relied upon by some users.

Adding the ``FAKE_GLOBALS_VALUE`` format
========================================
Adding the ``VALUE_WITH_FAKE_GLOBALS`` format
=============================================

:pep:`649` specifies:

Expand All @@ -584,25 +584,29 @@ CPython-specific and the documentation :py:ref:`explicitly warns <inspect-module
against relying on the values.

Larry Hastings suggested an alternative approach that does not
rely on code flags: a fourth format, ``FAKE_GLOBALS_VALUE``.
rely on code flags: a fourth format, ``VALUE_WITH_FAKE_GLOBALS``.
Compiler-generated annotate functions would support only the
``VALUE`` and ``FAKE_GLOBALS_VALUE`` formats, both of which are
``VALUE`` and ``VALUE_WITH_FAKE_GLOBALS`` formats, both of which are
implemented identically. The standard library would use the
``FAKE_GLOBALS_VALUE`` format when invoking an annotate function
``VALUE_WITH_FAKE_GLOBALS`` format when invoking an annotate function
in one of the special "fake globals" environments.

This approach is useful as a forward-compatible mechanism for
adding new annotation formats in the future. Users who manually
write annotate functions should raise ``NotImplementedError`` if
the ``FAKE_GLOBALS_VALUE`` format is requested, so the standard
the ``VALUE_WITH_FAKE_GLOBALS`` format is requested, so the standard
library will not call the manually written annotate function with
"fake globals", which could have unpredictable results.

Specification
-------------

An additional format, ``FAKE_GLOBALS_VALUE``, is added to the ``Format`` enum in the
``annotationlib`` module, with value equal to 4. Compiler-generated
``annotationlib`` module, with value equal to 2. (As a result, the values of the
other formats will shift relative to PEP 649: ``FORWARDREF`` will be 3 and ``SOURCE``
will be 4.)

Compiler-generated
annotate functions will support this format and return the same value as
they would return for the ``VALUE`` format. The standard library will pass
this format to the ``__annotate__`` function when it is called in a "fake globals"
Expand Down

0 comments on commit 390207b

Please sign in to comment.