@@ -81,30 +81,27 @@ variable or attribute should not be reassigned, redefined, or overridden.
8181Syntax
8282^^^^^^
8383
84- ``Final `` may be used in one of several forms:
84+ ``Final `` may be used in one of two forms:
8585
86- * With an explicit type, using the syntax ``Final[<type>] ``. Example::
86+ * As an :term: `explicitly typed Final `, using the syntax ``Final[<type>] ``.
87+ Example::
8788
8889 ID: Final[float] = 1
8990
90- * With no type annotation. Example::
91+ In class bodies and stub files the right hand side can be omitted::
92+
93+ ID: Final[float]
94+
95+ * As a :term: `bare Final `, without a type argument. Example::
9196
9297 ID: Final = 1
9398
99+ Bare ``Final`` annotations must have an assigned value.
100+
94101 The typechecker should apply its usual type inference mechanisms to
95102 determine the type of ``ID`` (here, likely, ``int``). Note that unlike for
96103 generic classes this is *not* the same as ``Final[Any]``.
97104
98- * In class bodies and stub files you can omit the right hand side and just write
99- ``ID: Final[float] ``. If the right hand side is omitted, there must
100- be an explicit type argument to ``Final ``.
101-
102- * Finally, as ``self.id: Final = 1 `` (also optionally with a type in
103- square brackets). This is allowed *only * in ``__init__ `` methods, so
104- that the final instance attribute is assigned only once when an
105- instance is created.
106-
107-
108105Semantics and examples
109106^^^^^^^^^^^^^^^^^^^^^^
110107
@@ -156,8 +153,8 @@ be initialized in the ``__init__`` method (except in stub files)::
156153 self.x = 1 # Good
157154
158155The generated ``__init__ `` method of :doc: `dataclasses ` qualifies for this
159- requirement: a bare ``x: Final[int] `` is permitted in a dataclass body, because
160- the generated ``__init__ `` will initialize ``x ``.
156+ requirement: an ``x: Final[int] `` without an initializer is permitted in a
157+ dataclass body, because the generated ``__init__ `` will initialize ``x ``.
161158
162159Type checkers should infer a final attribute that is initialized in a class
163160body as being a class variable, except in the case of :doc: `dataclasses `, where
@@ -167,6 +164,15 @@ necessary to create a final class variable with value ``3``. In
167164non-dataclasses, combining ``ClassVar `` and ``Final `` is redundant, and type
168165checkers may choose to warn or error on the redundancy.
169166
167+ ``Final `` attributes can also be declared in ``__init__ `` methods, as
168+ annotations on an assignment to an attribute on ``self ``. This is allowed
169+ *only * in ``__init__ ``, not in any other method::
170+
171+ class ImmutableAttributes:
172+ def __init__(self) -> None:
173+ self.x: Final = 3
174+ self.y: Final[str] = "knight"
175+
170176``Final `` may only be used in assignments or variable annotations. Using it in
171177any other position is an error. In particular, ``Final `` can't be used in
172178annotations for function arguments::
@@ -193,9 +199,9 @@ with ``Final`` to prevent mutating such values::
193199 z: Final = ('a', 'b') # Also works
194200
195201
196- Type checkers should treat uses of a final name that was initialized
197- with a literal as if it was replaced by the literal. For example, the
198- following should be allowed::
202+ Type checkers should treat uses of names declared with a :term: ` bare Final `
203+ that was initialized with a literal as if it was replaced by the literal. For
204+ example, the following should be allowed::
199205
200206 from typing import NamedTuple, Final
201207
0 commit comments