Skip to content

Commit 0aff0b1

Browse files
committed
Feedback
1 parent af1db03 commit 0aff0b1

1 file changed

Lines changed: 26 additions & 24 deletions

File tree

peps/pep-0823.rst

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ simplified example:
107107
@dataclass
108108
class Customer:
109109
user: User | None
110-
shopping_card: list[Item]
110+
shopping_cart: list[Item]
111111
112112
@dataclass
113113
class User:
@@ -181,7 +181,7 @@ least for dictionaries a useful helper method is ``dict.get(key)``.
181181
182182
class Customer(TypedDict):
183183
user: User | None
184-
shopping_card: list[Item]
184+
shopping_cart: list[Item]
185185
186186
class User(TypedDict):
187187
name: str
@@ -191,15 +191,15 @@ least for dictionaries a useful helper method is ``dict.get(key)``.
191191
if customer is not None:
192192
user = customer["user"]
193193
if user is not None:
194-
return user.name.lower()
194+
return user["name"].lower()
195195
return None
196196
197197
Writing it using ``?.`` and ``?[ ]`` would look like this:
198198

199199
::
200200

201201
def get_customer_name(data: Data) -> str | None:
202-
return data.get("customer")?["user"]?.name.lower()
202+
return data.get("customer")?["user"]?["name"].lower()
203203

204204
Other common patterns
205205
---------------------
@@ -217,7 +217,7 @@ hide in plain sight. Attribute and function names have been shortened.
217217
falsy values, e.g. ``False``, ``""``, ``0``, ``[]``, ``{}`` or custom
218218
objects which overwrite ``__bool__``, are filtered out too though.
219219
If code relied on this property, the expression cannot necessarily
220-
be replaced with ``?.`` or ``.[ ]``.
220+
be replaced with ``?.`` or ``?[ ]``.
221221

222222
::
223223

@@ -298,7 +298,7 @@ attribute or subscript access were used.
298298
# base?.tail
299299
_t.tail if ((_t := base) is not None) else None
300300
301-
The ``base`` can be replace with any number of expressions, including
301+
The ``base`` can be replaced with any number of expressions, including
302302
`Parenthesized ones <Parenthesized expressions - groupings_>`_
303303
while the ``tail`` is limited to attribute access, subscript,
304304
their ``None``-aware variants and call expressions.
@@ -324,11 +324,12 @@ Short-circuiting
324324

325325
If the left hand side (the ``base``) for ``?.`` or ``?[ ]`` evaluates to
326326
``None``, the remaining expression (the ``tail``) is skipped and the
327-
result will be set to ``None`` instead. The ``AttributeError`` for
328-
accessing a member of ``None`` or ``TypeError`` for trying to subscribe
329-
to ``None`` are omitted. It is therefore not necessary to change
330-
subsequent ``.`` or ``[ ]`` on the right hand side just because a
331-
``?.`` or ``?[ ]`` is used prior.
327+
result will be set to ``None`` instead. This includes everything in the
328+
tail part, e.g. evaluating function arguments or subscripts. The
329+
``AttributeError`` for accessing a member of ``None`` or ``TypeError``
330+
for trying to get a subscript of ``None`` are omitted. It is therefore
331+
not necessary to change subsequent ``.`` or ``[ ]`` on the right hand
332+
side just because a ``?.`` or ``?[ ]`` is used prior.
332333

333334
::
334335

@@ -384,7 +385,7 @@ in case the first part evaluates to ``None``.
384385
(a.b?.c or d).e?.func()
385386

386387
# a.b?.c
387-
_t2 = _t1.c if ((t1 := a.b) is not None) else None
388+
_t2 = _t1.c if ((_t1 := a.b) is not None) else None
388389

389390
# (... or d)
390391
_t3 = _t2 if _t2 else d
@@ -495,7 +496,7 @@ Multiline formatting
495496
Using two separate tokens to express ``?.`` and ``?[`` allows developers
496497
to insert a space or line break as needed. For multiline expressions it
497498
enables that ``?`` is appended to the ``optional`` subexpression whereas
498-
``.`` or ``[`` could be moved to the next line. This is indented merely
499+
``.`` or ``[`` could be moved to the next line. This is intended merely
499500
as an option for developers. Everyone is free to choose a style that fits
500501
their needs, especially code formatters might prefer a style which
501502
conforms better to their existing preferences. An example of what
@@ -937,20 +938,21 @@ might be too easy to miss besides "normal" attribute access and subscript
937938
operators.
938939

939940
This is a valid concern. Especially for long lines, it is not difficult
940-
to imaging a ``?`` hiding somewhere. However, as with all proposals the
941-
downsides have to be weight against the alternatives. As shown in the
941+
to imagine a ``?`` hiding somewhere. However, as with all proposals the
942+
downsides have to be weighed against the alternatives. As shown in the
942943
`Motivation`_ section, accessing nested values from objects with
943944
``optional`` attributes can be quite cumbersome. Getting all steps right,
944945
often involves a combination of assignment expressions, temporary
945946
variables and chained conditionals. Especially for beginners this can be
946-
overwhelming and it is frequently just faster to repeat each subexpression
947-
as well as only relying on the implicit ``bool(...) is True`` check instead
948-
of ``is not None``, which can fail in unexpected ways. It is also
949-
a bit slower. Even if the statements and expressions are written
950-
correctly, reading them again is far from simple.
947+
overwhelming and it is frequently just easier to repeat each subexpression
948+
as well as only relying on the implicit ``bool(...) is True`` check, which
949+
can fail in unexpected ways, instead of ``is not None``. It is also
950+
a bit slower since the expressions are not cached and evaluated again
951+
for every step. Additionally, even if the statements and expressions are
952+
written correctly, reading them again is far from simple.
951953

952954
In contrast, ``?.`` and ``?[ ]`` leave the core of the expression mostly
953-
untouched. It is thus fairly strait forward to see what is happening.
955+
untouched. It is thus fairly straight forward to see what is happening.
954956
Furthermore, it will be easier to write since one can start from the
955957
normal attribute access and subscript operators and just insert ``?``
956958
as needed. The Python error messages for accessing a member or subscript
@@ -988,7 +990,7 @@ attributes, i.e. do ``getattr(obj, attr, None) is not None``.
988990

989991
It was decided that the ``None``-aware operators should only handle
990992
``optional`` attributes, see the `Exception-aware operators`_ section
991-
section for more details why the latter interpretation was rejected.
993+
for more details why the latter interpretation was rejected.
992994

993995
Similar to `Easy to get ?. wrong`_ the discussion created a lot of
994996
confusion what the agreed upon interpretation should be. This will also
@@ -1082,13 +1084,13 @@ out for, only using keyword attributes for class patterns, making sure
10821084
the attribute names are correct since the class pattern can also match
10831085
``missing`` once and as such will not emit an error if it is misspelled,
10841086
and the performance impact from an often times unnecessary ``isinstance``
1085-
check. Furthermore, great care must be taking during refactorings as
1087+
check. Furthermore, great care must be taken during refactorings as
10861088
patterns often can not be updated automatically.
10871089

10881090
While the match statement has been available in Python since 3.10,
10891091
anecdotal evidence suggests that developers still prefer other
10901092
alternatives for ``optional`` attributes, at least for simple,
1091-
strait-forward expression. Pattern matching starts to become much more
1093+
straight-forward expression. Pattern matching starts to become much more
10921094
useful once multiple attributes or values need to be checked at the
10931095
same time.
10941096

0 commit comments

Comments
 (0)