Skip to content

Commit d305adf

Browse files
committed
More minor improvements
1 parent 23bce9e commit d305adf

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

peps/pep-0999.rst

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This PEP proposes adding two new operators.
1818
* The "``None``-aware attribute access" operator ``?.`` ("maybe dot")
1919
* The "``None``-aware indexing" operator ``?[ ]`` ("maybe subscript")
2020

21-
Both operators evaluate the left hand side, check if it is not ``None``
21+
Both operators evaluate the left hand side, check if it is ``not None``
2222
and only then evaluate the full expression. They are roughly equivalent
2323
to:
2424

@@ -77,14 +77,14 @@ for addition to the Python language. In contrast to :pep:`505`, it will
7777
only focus on the two access operators. See the `Deferred Ideas`_ section
7878
for more details.
7979

80-
``None`` aware access operators are not a new invention. Several other
80+
``None``-aware access operators are not a new invention. Several other
8181
modern programming languages have so called "``null``-aware" or
8282
"optional chaining" operators, including TypeScript [#ts]_,
8383
ECMAScript (a.k.a. JavaScript) [#js]_, C# [#csharp]_, Dart [#dart]_,
8484
Swift [#swift]_, Kotlin [#kotlin]_, Ruby [#ruby]_, PHP [#php]_ and more.
8585

8686
The general idea is to provide access operators which can traverse
87-
``null`` or ``None`` values without raising exceptions.
87+
``None`` values without raising exceptions.
8888

8989
Nested objects with ``optional`` attributes
9090
-------------------------------------------
@@ -133,7 +133,7 @@ correct things intuitively.
133133
return None
134134
135135
A simple function which will most likely work just fine. However, there
136-
are a few subtle issues. For one each condition only checks for truthiness.
136+
are a few subtle issues. For one, each condition only checks for truthiness.
137137
Would for example ``Machine`` overwrite ``__bool__`` to return ``False`` at
138138
some point, the function would just return ``None``. This is problematic
139139
since ``None`` is a valid return value already. Thus this would not raise
@@ -208,7 +208,7 @@ If any attribute names change, the match statement needs to be
208208
updated as well. Even IDEs can not reliably do that themselves since a
209209
class pattern is not restricted to existing attributes and can instead
210210
match any possible name. For sequence patterns it is also necessary
211-
to remember the wildcard match. Lastly, using ``match`` is significantly
211+
to remember the wildcard pattern. Lastly, using ``match`` is significantly
212212
slower because for each class pattern an ``isinstance`` check is performed
213213
first. This could be somewhat mitigated by using ``object(...)`` instead,
214214
though reading the pattern would be considerably more difficult.
@@ -347,7 +347,7 @@ Other common patterns
347347

348348
A collection of additional patterns which could be improved with
349349
``?.`` and ``?[ ]``. It is not the goal to list every foreseeable
350-
option but rather to help recognize these patterns which often
350+
use case but rather to help recognize these patterns which often
351351
hide in plain sight. Attribute and function names have been shortened.
352352

353353
.. note::
@@ -394,7 +394,7 @@ hide in plain sight. Attribute and function names have been shortened.
394394

395395
d2: dict
396396
key in d2 and d2[key][other]
397-
d.get(key)?[other]
397+
d2.get(key)?[other]
398398

399399
key in d2 and d2[key].do_something()
400400
d2.get(key)?.do_something()
@@ -417,10 +417,10 @@ The maybe-dot and maybe-subscript operators
417417

418418
Two new operators are added, ``?.`` ("maybe-dot") and ``?[ ]``
419419
("maybe subscript"). Both operators first evaluate the left hand side
420-
(the ``base``). The result is stored in a temporary variable, so that
421-
the expression is not evaluated again. It is checked if the result is
422-
not ``None`` and only then is the remaining expression (the ``tail``)
423-
evaluated as if normal attribute or subscript access were used.
420+
(the ``base``). The result is cached, so that the expression is not
421+
evaluated again. It is checked if the result is not ``None`` and only
422+
then is the remaining expression (the ``tail``) evaluated as if normal
423+
attribute or subscript access were used.
424424

425425
.. code-block:: python
426426
@@ -450,13 +450,13 @@ their ``None``-aware variants and call expressions.
450450
Short-circuiting
451451
****************
452452

453-
If the left hand side (the ``base``) for ``?.`` or ``?[ ]`` evaluate to
453+
If the left hand side (the ``base``) for ``?.`` or ``?[ ]`` evaluates to
454454
``None``, the remaining expression (the ``tail``) is skipped and the
455455
result will be set to ``None`` instead. The ``AttributeError`` for
456456
accessing a member of ``None`` or ``TypeError`` for trying to subscribe
457-
to ``None`` are omitted. It is therefore not necessary to change ``.``
458-
or ``[ ]`` on the right hand side just because a ``?.`` or ``?[ ]`` is
459-
used prior.
457+
to ``None`` are omitted. It is therefore not necessary to change
458+
subsequent ``.`` or ``[ ]`` on the right hand side just because a
459+
``?.`` or ``?[ ]`` is used prior.
460460

461461
::
462462

@@ -676,8 +676,8 @@ Deferred Ideas
676676
Coalesce ``??`` and coalesce assignment operator ``??=``
677677
--------------------------------------------------------
678678

679-
:pep:`505` also suggested the addition of a ``None`` coalescing operator
680-
``??`` and a coalesce assignment operator ``??=``. As the ``None``-aware
679+
:pep:`505` also suggested the addition of a "``None`` coalescing" operator
680+
``??`` and a "coalescing assignment" operator ``??=``. As the ``None``-aware
681681
access operators have their own use cases, the coalescing operators were
682682
moved into a separate document, see PEP-XXX. Both proposals can be
683683
adopted independently of one another.
@@ -710,7 +710,7 @@ could further make parsing of structured data easier since it would no
710710
longer be necessary to check if a ``list`` or ``tuple`` is long enough
711711
before trying to access the n-th element avoiding a possible
712712
``IndexError``. While potentially useful, the idea is out of the scope
713-
of this PEP.
713+
for this PEP.
714714

715715

716716
Rejected Ideas
@@ -744,7 +744,7 @@ there are used. If multiple attributes in an expression can return ``None``,
744744
it might be necessary to add them multiple times ``a?.b.c?[0].d?.e()``.
745745
It was suggested to instead add a new soft-keyword ``maybe`` to prefix
746746
the expression: ``maybe a.b.c[0].d.e()``. A ``None`` check would then be
747-
added for attribute and item access automatically.
747+
added for each attribute and item access automatically.
748748

749749
While this might be easier to write at first, it introduces new issues.
750750
When using explicit ``?.`` and ``?[ ]`` operators, the input space is well
@@ -1098,9 +1098,9 @@ A lot of the discussion centered around the interpretation of the
10981098
i.e. perform ``is not None`` checks, or also work for ``missing``
10991099
attributes, i.e. do ``getattr(obj, attr, None) is not None``.
11001100

1101-
It was agreed that the operators should only handle ``optional``
1102-
attributes, see the `Exception-aware operators`_ section section
1103-
for more details why the latter interpretation was rejected.
1101+
It was agreed that the ``None``-aware operators should only handle
1102+
``optional`` attributes, see the `Exception-aware operators`_ section
1103+
section for more details why the latter interpretation was rejected.
11041104

11051105
Similar to `Easy to get ?. wrong`_ the discussion created a lot of
11061106
confusion what the agreed upon interpretation should be. This will also
@@ -1191,8 +1191,8 @@ While the match statement has been available in Python since 3.10,
11911191
anecdotal evidence suggests that developers still prefer other
11921192
alternatives for ``optional`` attributes, at least for simple,
11931193
strait-forward expression. Pattern matching starts to become much more
1194-
useful once multiple attributes or values on a same level need to be
1195-
checked.
1194+
useful once multiple attributes or values need to be checked at the
1195+
same time.
11961196

11971197
... try ... except ...
11981198
**********************
@@ -1277,13 +1277,6 @@ provide a meaning for these operators which roughly matches those in
12771277
other languages while still respecting the norms in Python itself.
12781278

12791279

1280-
Open Issues
1281-
===========
1282-
1283-
* Should ``None``-aware function calls be added to the proposal?
1284-
They are supported in JavaScript [#js]_.
1285-
1286-
12871280
Footnotes
12881281
=========
12891282

0 commit comments

Comments
 (0)