8383
8484``Final `` may be used in one of two forms:
8585
86- * As :term: `explicitly typed Final `, using the syntax ``Final[<type>] ``.
86+ * As an :term: `explicitly typed Final `, using the syntax ``Final[<type>] ``.
8787 Example::
8888
8989 ID: Final[float] = 1
@@ -92,10 +92,12 @@ Syntax
9292
9393 ID: Final[float]
9494
95- * As :term: `bare Final `, with no type annotation . Example::
95+ * As a :term: `bare Final `, without a type argument . Example::
9696
9797 ID: Final = 1
9898
99+ Bare ``Final`` annotations must have an assigned value.
100+
99101 The typechecker should apply its usual type inference mechanisms to
100102 determine the type of ``ID`` (here, likely, ``int``). Note that unlike for
101103 generic classes this is *not* the same as ``Final[Any]``.
@@ -150,18 +152,9 @@ be initialized in the ``__init__`` method (except in stub files)::
150152 def __init__(self) -> None:
151153 self.x = 1 # Good
152154
153- Alternatively, ``Final `` attributes can be assigned in and *only * in
154- ``__init__ `` methods, so that the final instance attribute is assigned only
155- once when an instance is created::
156-
157- class ImmutableAttributes:
158- def __init__(self) -> None:
159- self.x: Final = 3
160- self.y: Final[str] = "knight"
161-
162155The generated ``__init__ `` method of :doc: `dataclasses ` qualifies for this
163- requirement: a ``x: Final[int] `` without right side is permitted in a dataclass
164- body, because 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 ``.
165158
166159Type checkers should infer a final attribute that is initialized in a class
167160body as being a class variable, except in the case of :doc: `dataclasses `, where
@@ -171,6 +164,15 @@ necessary to create a final class variable with value ``3``. In
171164non-dataclasses, combining ``ClassVar `` and ``Final `` is redundant, and type
172165checkers may choose to warn or error on the redundancy.
173166
167+ ``Final `` attributes can also be declared in and *only * in ``__init__ ``
168+ methods, so that the final instance attribute is assigned only once when an
169+ instance is created::
170+
171+ class ImmutableAttributes:
172+ def __init__(self) -> None:
173+ self.x: Final = 3
174+ self.y: Final[str] = "knight"
175+
174176``Final `` may only be used in assignments or variable annotations. Using it in
175177any other position is an error. In particular, ``Final `` can't be used in
176178annotations for function arguments::
@@ -197,9 +199,9 @@ with ``Final`` to prevent mutating such values::
197199 z: Final = ('a', 'b') # Also works
198200
199201
200- Type checkers should treat uses of a final name that was initialized
201- with a literal as if it was replaced by the literal. For example, the
202- following should be allowed::
202+ Type checkers should treat uses of name 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::
203205
204206 from typing import NamedTuple, Final
205207
0 commit comments