PEP 823: None-aware access operators#4798
Conversation
|
@gvanrossum Please can you confirm you're sponsoring this PEP? |
Yes. |
gvanrossum
left a comment
There was a problem hiding this comment.
Here are a ton of nits. I am strongly in favor of this proposal -- I just think the main exposition is a bit unwieldy. Thanks for writing this Marc!
Note that I didn't get to the Deferred/Rejected ideas yet. Some of the things I balk over in the main exposition (like using the walrus or match/case) might find a place in the latter.
|
|
||
| An attribute or value is ``optional`` | ||
| In the context of this PEP an attribute or value is considered | ||
| ``optional`` if it is always present but can be ``None``. |
There was a problem hiding this comment.
Nice -- this matches exactly what Optional[X] means in the type notation.
|
|
||
| After students know how the attribute access ``.`` and subscript ``[ ]`` | ||
| operators work, they may learn about the "``None``-aware" versions for | ||
| both. |
There was a problem hiding this comment.
Honestly I'd only teach them ?. and wait until they ask if there's an equivalent for [ ] before telling them that.
cdce8p
left a comment
There was a problem hiding this comment.
Thanks for the detailed feedback! I'd like to address your general comments regarding the motivation section here directly instead of splitting it up for each comment.
While I was preparing to write this PEP, I reread a lot of the discussions from the last few years. One of the most common arguments against these operators was that they are basically just syntactic sugar and don't add anything new. Technically speaking this is correct. We both know that these would still be great additions and make certain coding patterns a lot easier to write. However, the question I asked myself was how can I best explain why all the other patterns are only suboptimal.
I ended up with the structure you read. It starts at "beginner" level with simple truthiness checks. I've actually seen code like this in production. Sometimes lazy code is enough even if it contains problems. It continues with the explicit is not None checks, before ultimately adding the assignment expressions as well. try and match were added as these were the other often suggested builtin alternatives.
By showcasing how each of these work, I can point out the subtle issues each option has and provide a justification why ?. and ?[ ] should be added.
In the end I also wanted to avoid falling into the same, IMO trap, PEP 505 did with just showing nice examples and basically saying the new operators are just better. https://peps.python.org/pep-0505/#examples That's also why I deliberately moved the common patterns towards the end of the motivation section.
--
I admit the example isn't ideal. It's adopted from a discussion post I saw. However, I wanted to add some "real" class and variable names for these examples. Of course, a?.b.c?[d].e would work as well, but that oftentimes seems contrived and far away. PEP 505 did that and I always found that somewhat unintuitive, even though it's strait forward. https://peps.python.org/pep-0505/#the-maybe-dot-and-maybe-subscript-operators
I'm happy to change it if we can come up with a better example.
You're right to push back -- am taking a pretty extreme POV here. About syntactic sugar: half of Python is syntactic sugar. We don't need My main feeling about the long exposition in the Motivation section is that many of the things you show are either just bad habits (no matter how often people write them -- this PEP isn't the place to educate them), or straw-men examples that look like they are only shown to shoot them down (the match/case version). If people have brought these up in the discussion and you want to capture that, maybe you can move them to the "Rejected ideas" section with subheadings like "Do nothing -- use match/case (or try/except, or whatever other suggestion was made)". How do you feel about that option? If you don't like that either, I'll approve your current approach -- I don't want to die on this hill. :-) |
I'll think about this some more. You might not have gotten around to it yet but I do also have added a |
|
Just a note that this PEP (and #4799) is targeting Python 3.15 and the feature freeze is a couple of months away. And these PRs will need to be merged, discussed/revised, submitted to the SC, approved, and implemented before then if they're to make 3.15, so there's not a huge amount of time. |
Thanks for the reminder @hugovk. I was thinking about how to rewrite the motivation section based on the feedback but then life happened. I do plan to finish things up for the next round of reviews within the next few days, hopefully this weekend. I'm aware that the time until the feature freeze is quite short. Thankfully the technical questions are AFAIK settled now after the intense prior discussions the last few months. Though of course there is always the possibility that something else comes up after these two have been posted. |
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Documentation build overview
19 files changed ·
|
|
Sorry for the long delay here. I pushed it back, after it became clear that I won't had the time to finish it for the 3.15 release. Back at it again now. Took some time to rewrite most of the motivation section including a different / simpler example. The whole section got a lot shorter as well. The detours regarding try-except and match are already covered in the later "Common objections" sections. Also went through the remaining review comments and addressed most of them. Will look at the feedback and update the draft for PEP 824 next. |
Basic requirements (all PEP Types)
pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) andPEPheaderAuthororSponsor, and formally confirmed their approvalAuthor,Status(Draft),TypeandCreatedheaders filled out correctlyPEP-Delegate,Topic,RequiresandReplacesheaders completed if appropriate.github/CODEOWNERSfor the PEPStandards Track requirements
Python-Versionset to valid (pre-beta) future Python version, if relevantDiscussions-ToandPost-History📚 Documentation preview 📚: https://pep-previews--4798.org.readthedocs.build/