From 750fa41357c7f00c66f280393f05db00cdf23e1e Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 1 Mar 2025 08:48:54 -0700 Subject: [PATCH 1/2] Added "Importing Final Variables" section --- docs/spec/qualifiers.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/spec/qualifiers.rst b/docs/spec/qualifiers.rst index 7ba4d9f4..3ff8e4d6 100644 --- a/docs/spec/qualifiers.rst +++ b/docs/spec/qualifiers.rst @@ -207,6 +207,34 @@ following should be allowed:: item or a :ref:`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`` From 4b81b9d3cfc6e83aaa38ede7f494518573cfaa78 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sun, 2 Mar 2025 12:51:04 -0700 Subject: [PATCH 2/2] Clarified that this rule applies only to an import statement. --- docs/spec/qualifiers.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/spec/qualifiers.rst b/docs/spec/qualifiers.rst index 3ff8e4d6..d6105c7b 100644 --- a/docs/spec/qualifiers.rst +++ b/docs/spec/qualifiers.rst @@ -212,9 +212,9 @@ 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:: +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