Skip to content

Commit 811a867

Browse files
authored
Added "Importing Final Variables" section (#1937)
* Added "Importing Final Variables" section * Clarified that this rule applies only to an import statement.
1 parent 8d3f041 commit 811a867

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/spec/qualifiers.rst

+28
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@ following should be allowed::
207207
item or a :ref:`NamedTuple <namedtuple>` field. Such usage also generates
208208
an error at runtime.
209209

210+
211+
Importing ``Final`` Variables
212+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
213+
214+
If a module declares a ``Final`` variable and another module imports that
215+
variable in an import statement by name or wildcard, the imported symbol
216+
inherits the ``Final`` type qualifier. Any attempt to assign a different value
217+
to this symbol should be flagged as an error by a type checker::
218+
219+
# lib/submodule.py
220+
from typing import Final
221+
PI: Final = 3.14
222+
223+
# lib/__init__.py
224+
from .submodule import PI # PI is Final
225+
226+
# test1.py
227+
from lib import PI
228+
PI = 0 # Error: Can't assign to Final value
229+
230+
from lib import PI as PI2
231+
PI2 = 0 # Error: Can't assign to Final value
232+
233+
# test2.py
234+
from lib import *
235+
PI = 0 # Error: Can't assign to Final value
236+
237+
210238
.. _`annotated`:
211239

212240
``Annotated``

0 commit comments

Comments
 (0)