You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: peps/pep-0767.rst
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -281,9 +281,12 @@ Assignment to a read-only instance attribute must be allowed in the following co
281
281
282
282
* In ``__init__``, on the instance of the declaring class received as
283
283
the first parameter (usually ``self``).
284
-
* In ``__new__`` and ``@classmethod``\ s, on instances of the declaring class
285
-
created via a call to ``__new__`` on any object of type ``type[T]``, where
286
-
``T`` is a *nominal supertype* of the declaring class.
284
+
* In ``__new__`` and ``@classmethod``\ s, on instances of the declaring class created via:
285
+
286
+
- a call to ``super().__new__()``,
287
+
- a call to ``__new__`` on any object of type ``type[T]``,
288
+
where ``T`` is a *nominal supertype* of the declaring class.
289
+
287
290
* At declaration in the class scope.
288
291
289
292
Additionally, a type checker may choose to allow the assignment
@@ -421,6 +424,12 @@ is assignable to ``T``:
421
424
422
425
Read-only protocol attributes may not be assigned to or deleted in any context.
423
426
427
+
Note that when inheriting from a protocol to `explicitly declare its implementation <https://typing.python.org/en/latest/spec/protocol.html#explicitly-declaring-implementation>`__,
428
+
for the purpose of applying rules regarding read-only attributes (that the protocol may define),
429
+
the protocol should be treated as if it was a nominal class.
430
+
In particular, this means that subclasses *can* initialize read-only attributes
431
+
that have been defined by the protocol.
432
+
424
433
Type checkers should not assume that access to a protocol's read-only attributes
425
434
is supported by the protocol's type (``type[HasName]``). Even if an attribute
426
435
exists on the protocol's type, no assumptions should be made about its type.
@@ -436,8 +445,8 @@ Subtyping
436
445
The inability to assign to or delete read-only attributes makes them covariant.
437
446
This has a few subtyping implications. Borrowing from :pep:`705#inheritance`:
438
447
439
-
* Read-only attributes can be redeclared as writable attributes, descriptors
440
-
or class variables::
448
+
* Read-only attributes can be redeclared by a subclass as writable attributes,
0 commit comments