Skip to content

Commit 750fa41

Browse files
committed
Added "Importing Final Variables" section
1 parent fafcdeb commit 750fa41

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/spec/qualifiers.rst

Lines changed: 28 additions & 0 deletions
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 by name or by wildcard import, the imported symbol inherits the
216+
``Final`` type qualifier. Any attempt to assign a different value to this
217+
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)