Conversation
Clarify that bare `ReadOnly` is not allowed, and add that to rejected ideas Specify that `type[Protocol]` does not inherit the protocol's attributes Close issue: "Extending Initialization" Bikeshed on some wording Use my real name
peps/pep-0767.rst
Outdated
| self.name: ReadOnly[str] = name | ||
|
|
||
| Use of bare ``ReadOnly`` (without ``[<type>]``) is not allowed. | ||
| Type checkers should error on any attempt to reassign or ``del``\ ete an attribute |
There was a problem hiding this comment.
The meaning of "reassign" here is not clear. (What is an "assignment" and what is a "reassignment"?)
I think it's clarified below, but maybe we should reference that here.
peps/pep-0767.rst
Outdated
| ''''''''''''''''''' | ||
|
|
||
| Assignment to an instance attribute must be allowed in the following contexts: | ||
| Assignment to a read-only instance attribute must be allowed in the following contexts: |
There was a problem hiding this comment.
I think @oscarbenjamin raised an interesting point in the Discourse thread about this. The effects of ReadOnly as described in this PEP (structural typing and variance of generics) only require for soundness that we disallow external mutation of the attribute. Disallowing internal mutation (within methods of the class) is not required for soundness, and is arguably veering too far into Final territory.
It would simplify this section (and type checker implementations) quite a lot if we just specified that ReadOnly describes the external interface of the attribute, and all internal reassignments of a ReadOnly attribute (within methods of the class) are permitted.
There was a problem hiding this comment.
I think I was wrong here, unfortunately -- allowing internal mutation is also unsound.
hugovk
left a comment
There was a problem hiding this comment.
Where are we up to with this PR? Can we merge it? Let's do so if there's no further feedback in ~a week.
| PEP: 767 | ||
| Title: Annotating Read-Only Attributes | ||
| Author: Eneg <eneg at discuss.python.org> | ||
| Author: Łukasz Modzelewski <eneg at discuss.python.org> |
There was a problem hiding this comment.
Is this a real email address? If not, you can also leave it blank.
There was a problem hiding this comment.
It's not a real email; one can reach out to me on https://discuss.python.org/
Do you think I should remove it?
There was a problem hiding this comment.
Yes please.
| Author: Łukasz Modzelewski <eneg at discuss.python.org> | |
| Author: Łukasz Modzelewski |
ReadOnlyis not allowed, and added that to rejected ideastype[Protocol])📚 Documentation preview 📚: https://pep-previews--4559.org.readthedocs.build/