Skip to content
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

Merged
merged 20 commits into from
Dec 4, 2024

Conversation

Enegg
Copy link
Contributor

@Enegg Enegg commented Nov 19, 2024

Basic requirements (all PEP Types)

  • Read and followed PEP 1 & PEP 12
  • File created from the latest PEP template
  • PEP has next available number, & set in filename (pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) and PEP header
  • Title clearly, accurately and concisely describes the content in 79 characters or less
  • Core dev/PEP editor listed as Author or Sponsor, and formally confirmed their approval
  • Author, Status (Draft), Type and Created headers filled out correctly
  • PEP-Delegate, Topic, Requires and Replaces headers completed if appropriate
  • Required sections included
    • Abstract (first section)
    • Copyright (last section; exact wording from template required)
  • Code is well-formatted (PEP 7/PEP 8) and is in code blocks, with the right lexer names if non-Python
  • PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
  • Authors/sponsor added to .github/CODEOWNERS for the PEP

Standards Track requirements

  • PEP topic discussed in a suitable venue with general agreement that a PEP is appropriate
  • Suggested sections included (unless not applicable)
    • Motivation
    • Rationale
    • Specification
    • Backwards Compatibility
    • Security Implications
    • How to Teach This
    • Reference Implementation
    • Rejected Ideas
    • Open Issues
  • Python-Version set to valid (pre-beta) future Python version, if relevant
  • Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
  • Right before or after initial merging, PEP discussion thread created and linked to in Discussions-To and Post-History

📚 Documentation preview 📚: https://pep-previews--4127.org.readthedocs.build/pep-0767/

Copy link

cpython-cla-bot bot commented Nov 19, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

peps/pep-9999.rst Outdated Show resolved Hide resolved
@AlexWaygood AlexWaygood requested a review from carljm November 19, 2024 09:32
Copy link
Member

@JelleZijlstra JelleZijlstra left a 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!

peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@rchen152 rchen152 left a 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!

peps/pep-9999.rst Outdated Show resolved Hide resolved
Copy link
Member

@carljm carljm left a 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.

peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
peps/pep-9999.rst Outdated Show resolved Hide resolved
@hugovk hugovk changed the title PEP 76X: Read-only class attributes PEP 767: Read-only class attributes Nov 20, 2024
peps/pep-9999.rst Outdated Show resolved Hide resolved
Enegg and others added 3 commits November 21, 2024 01:39
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
@Enegg Enegg changed the title PEP 767: Read-only class attributes PEP 767: Annotating Read-Only Attributes Nov 22, 2024
peps/pep-0767.rst Outdated Show resolved Hide resolved
Copy link
Member

@carljm carljm left a 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!

peps/pep-0767.rst Outdated Show resolved Hide resolved
peps/pep-0767.rst Outdated Show resolved Hide resolved
peps/pep-0767.rst Outdated Show resolved Hide resolved
peps/pep-0767.rst Outdated Show resolved Hide resolved
@Enegg
Copy link
Contributor Author

Enegg commented Nov 22, 2024

This is looking ready for merge and discussion to me

Does that mean I do not need to bother with the How to teach this section (for the time being)?
Should I remove it, or is it fine if the template stays?

@carljm
Copy link
Member

carljm commented Nov 22, 2024

Does that mean I do not need to bother with the How to teach this section (for the time being)?
Should I remove it, or is it fine if the template stays?

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 ReadOnly in TypedDict, and the close parallel to this usage.

We should also go through the remaining checkbox items in the summary and make sure they are all addressed.

@Enegg
Copy link
Contributor Author

Enegg commented Nov 24, 2024

I've noticed that covariant overrides of read-only attributes open a door for a type of bugs.
A subclass overriding a read-only attribute with a narrower type may incorrectly initialize it:

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 bool.__add__ side (or the +1?), or it could happen elsewhere?

@beauxq
Copy link

beauxq commented Nov 24, 2024

I've noticed that covariant overrides of read-only attributes open a door for a type of bugs. A subclass overriding a read-only attribute with a narrower type may incorrectly initialize it:

...

I wonder if what I've observed here is just an unsoundness on the bool.__add__ side (or the +1?), or it could happen elsewhere?

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.
If you're going to override it, you need to actually override it, not just say you're going to override it and then don't.

…adOnly; `__new__` and `classmethod` example via simplified Fraction; Note Final can override ReadOnly; Rejected Ideas x2; Expand on Extending Initialization
@Enegg Enegg marked this pull request as ready for review November 26, 2024 13:10
@Enegg Enegg requested a review from a team as a code owner November 26, 2024 13:10
@hugovk
Copy link
Member

hugovk commented Nov 26, 2024

(Conflict resolved)

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, almost there!

peps/pep-0767.rst Outdated Show resolved Hide resolved
peps/pep-0767.rst Outdated Show resolved Hide resolved
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
.. code-block:: python

class Patient:
number: ReadOnly[int] = 0
Copy link
Contributor

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).

Copy link

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)

@JelleZijlstra
Copy link
Member

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.

@JelleZijlstra JelleZijlstra merged commit ed44bb0 into python:main Dec 4, 2024
5 checks passed
gvanrossum pushed a commit to gvanrossum/peps that referenced this pull request Dec 10, 2024
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants