Skip to content

Commit

Permalink
Added "Importing Final Variables" section
Browse files Browse the repository at this point in the history
  • Loading branch information
erictraut committed Mar 1, 2025
1 parent fafcdeb commit 750fa41
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/spec/qualifiers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,34 @@ following should be allowed::
item or a :ref:`NamedTuple <namedtuple>` field. Such usage also generates
an error at runtime.


Importing ``Final`` Variables
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If a module declares a ``Final`` variable and another module imports that
variable by name or by wildcard import, the imported symbol inherits the
``Final`` type qualifier. Any attempt to assign a different value to this
symbol should be flagged as an error by a type checker::

# lib/submodule.py
from typing import Final
PI: Final = 3.14

# lib/__init__.py
from .submodule import PI # PI is Final

# test1.py
from lib import PI
PI = 0 # Error: Can't assign to Final value

from lib import PI as PI2
PI2 = 0 # Error: Can't assign to Final value

# test2.py
from lib import *
PI = 0 # Error: Can't assign to Final value


.. _`annotated`:

``Annotated``
Expand Down

0 comments on commit 750fa41

Please sign in to comment.