Skip to content

Commit

Permalink
pythongh-122937: Add docs about annotations evaluation order
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Aug 16, 2024
1 parent 786cac0 commit b219b82
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Doc/howto/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ Annotations Best Practices
in your code, please see the :mod:`typing` module.


.. _annotations-howto-general:

General rules
=============

When defining annotations, remember about the execution order. For example::

class User:
username: str = 'miss-islington'

1. Value ``'miss-islington'`` will be assigned
to name ``username`` in ``User`` class scope
2. Name ``str`` is evaluated in the annotated assignment statement
3. Name ``str`` is assigned as annotation
to ``username`` key in ``User.__annotations__``

This means that a code like ``int: int = 1`` will produce annotations like
``{"int": 1}`` because of how the execution order works.
If you need to shadow some names, use this approach:

.. doctest::

>>> from typing import TypeAlias

>>> int_: TypeAlias = int # type alias for future name shadowing

>>> class Response:
... int: int_ = 200

>>> Response.__annotations__
{'int': <class 'int'>}


Accessing The Annotations Dict Of An Object In Python 3.10 And Newer
====================================================================

Expand Down

0 comments on commit b219b82

Please sign in to comment.