Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Importing Final Variables" section #1937

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 in an import statement by name or wildcard, 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