-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PEP 767: Annotating Read-Only Attributes #4127
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this will be a great improvement to the type system!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it'll be a great addition!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work on the PEP! Some things to sort out, but overall it looks strong to me.
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Carl Meyer <carl@oddbird.net>
…ave a type, better immutability examples, yeet changes to Final
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking ready for merge and discussion to me; I'm sure PEP editors will be able find some things that should be fixed before merge, though :) Thanks for all the work on it!
Does that mean I do not need to bother with the |
I missed this, sorry. This section should be filled out. It doesn't need to be extensive for this PEP, I don't think. It should mention the existing use of We should also go through the remaining checkbox items in the summary and make sure they are all addressed. |
I've noticed that covariant overrides of read-only attributes open a door for a type of bugs. class Base:
foo: ReadOnly[int]
def __init__(self, foo: int) -> None:
self.foo = foo + 1 # True + 1 == 2
class Subtype(Base):
foo: bool # narrower type; lack of ReadOnly doesn't matter
def __init__(self, foo: bool) -> None:
super().__init__(foo) # is a call to super always safe?
assert isinstance(self.foo, bool) I wonder if what I've observed here is just an unsoundness on the |
This looks to me like it's the same as this. class B:
pass
class C(B):
pass
class Base:
foo: ReadOnly[B]
def __init__(self) -> None:
self.foo = B()
class Subtype(Base):
foo: C # narrower type
def __init__(self) -> None:
super().__init__()
assert isinstance(self.foo, C) What I see here is basically just saying we're overriding it without actually overriding it. |
…adOnly; `__new__` and `classmethod` example via simplified Fraction; Note Final can override ReadOnly; Rejected Ideas x2; Expand on Extending Initialization
(Conflict resolved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, almost there!
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
.. code-block:: python | ||
|
||
class Patient: | ||
number: ReadOnly[int] = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the semantics of omitting the type explained? Example:
class Patient:
number: ReadOnly = 0 # Is this ok? What's the type of number?
If type inference is supported, it would be useful to have some guidelines about how type checkers should approach this, even if some details are omitted (similar to other PEPs that don't specify type inference in detail).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some discussion on this here: #4127 (comment)
I'm going to merge this so we can put the initial version of the PEP up for discussion. Changes can still be made to the PEP before it's submitted to the Typing Council. |
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Carl Meyer <carl@oddbird.net> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Basic requirements (all PEP Types)
pep-NNNN.rst
), PR title (PEP 123: <Title of PEP>
) andPEP
headerAuthor
orSponsor
, and formally confirmed their approvalAuthor
,Status
(Draft
),Type
andCreated
headers filled out correctlyPEP-Delegate
,Topic
,Requires
andReplaces
headers completed if appropriate.github/CODEOWNERS
for the PEPStandards Track requirements
Python-Version
set to valid (pre-beta) future Python version, if relevantDiscussions-To
andPost-History
📚 Documentation preview 📚: https://pep-previews--4127.org.readthedocs.build/pep-0767/