@@ -250,20 +250,24 @@ Initialization
250250
251251Assignment to a read-only attribute can only occur in the class declaring the attribute.
252252There is no restriction to how many times the attribute can be assigned to.
253- The assignment can happen only \* in the following contexts:
253+ The assignment must be allowed in the following contexts:
254254
255- 1. In the body of ``__init__ ``, on the instance received as the first parameter (likely, ``self ``).
256- 2. In the body of ``__new__ ``, on instances of the declaring class created via
257- a direct call to a super-class' ``__new__ `` method.
258- 3. In the body of the class.
255+ 1. In ``__init__ ``, on the instance received as the first parameter (likely, ``self ``).
256+ 2. In ``__new__ ``, on instances of the declaring class created via a call
257+ to a super-class' ``__new__ `` method.
258+ 3. At declaration in the body of the class.
259259
260- \* A type checker may choose to allow assignment to read-only attributes on instances
261- of the declaring class in ``__new__ ``, without regard to the origin of the instance.
262- (This choice trades soundness, as the instance may already be initialized,
263- for the simplicity of implementation.)
260+ Additionally, a type checker may choose to allow the assignment:
264261
265- Note that child classes cannot assign to read-only attributes of parent classes
266- in any of the aforementioned contexts, unless the attributes are redeclared.
262+ 1. In ``__new__ ``, on instances of the declaring class, without regard
263+ to the origin of the instance.
264+ (This choice trades soundness, as the instance may already be initialized,
265+ for the simplicity of implementation.)
266+ 2. In ``@classmethod ``\ s, on instances of the declaring class created via
267+ a call to the class' or super-class' ``__new__ `` method.
268+
269+ Note that a child class cannot assign to any read-only attribute of a parent class
270+ in any of the aforementioned contexts, unless the attribute is redeclared.
267271
268272.. code-block :: python
269273
@@ -296,7 +300,7 @@ in any of the aforementioned contexts, unless the attributes are redeclared.
296300
297301 class SubBand (Band ):
298302 def __init__ (self ) -> None :
299- self .songs = [] # error: cannot assign to a read-only attribute of base class
303+ self .songs = [] # error: cannot assign to a read-only attribute of a base class
300304
301305 When a class-level declaration has an initializing value, it can serve as a `flyweight <https://en.wikipedia.org/wiki/Flyweight_pattern >`_
302306default for instances:
@@ -475,10 +479,9 @@ However, caution is advised while using the backported ``typing_extensions.ReadO
475479in older versions of Python. Mechanisms inspecting annotations may behave incorrectly
476480when encountering ``ReadOnly ``; in particular, the ``@dataclass `` decorator
477481which `looks for <https://docs.python.org/3/library/dataclasses.html#class-variables >`_
478- ``ClassVar `` may incorrectly treat ``ReadOnly[ClassVar[...]] `` as an instance attribute.
482+ ``ClassVar `` may mistakenly treat ``ReadOnly[ClassVar[...]] `` as an instance attribute.
479483
480- In such circumstances, authors should prefer ``ClassVar[ReadOnly[...]] `` over
481- ``ReadOnly[ClassVar[...]] ``.
484+ To avoid issues with introspection, use ``ClassVar[ReadOnly[...]] `` instead of ``ReadOnly[ClassVar[...]] ``.
482485
483486
484487Security Implications
0 commit comments