Skip to content

Commit 072c686

Browse files
committed
Change IndexError to TypeError
1 parent 3d0b0a1 commit 072c686

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

peps/pep-0999.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Nested objects with ``optional`` attributes
9191

9292
When writing Python code, it is common to encounter objects with ``optional``
9393
attributes. Accessing attributes, subscript or function calls can raise
94-
``AttributeError`` or ``IndexError`` at runtime if the value is ``None``.
94+
``AttributeError`` or ``TypeError`` at runtime if the value is ``None``.
9595
Several common patterns have developed to ensure these operations will
9696
not raise. The goal for ``?.`` and ``?[ ]`` is to make reading and writing
9797
these expressions much simpler while being predictable and doing the
@@ -185,7 +185,7 @@ significantly.
185185
def get_person_email(sensor: Sensor) -> str | None:
186186
try:
187187
return sensor.machine.line.department.engineer.emails[0]
188-
except AttributeError, IndexError:
188+
except AttributeError, TypeError:
189189
return None
190190
191191
Another approach would be to use a ``match`` statement instead. This
@@ -687,7 +687,7 @@ If ``.b`` all of the sudden is also ``None``, it would still raise an
687687
issues. As the expression output can already be ``None`` the space of
688688
potential output didn't change and as such no error would appear.
689689

690-
If it is the intend to catch all ``AttributeError`` and ``IndexError``,
690+
If it is the intend to catch all ``AttributeError`` and ``TypeError``,
691691
a try-except block can be used instead.
692692

693693
As the ``?.`` and ``?[ ]`` would allow developers to be more explicit in
@@ -1002,7 +1002,7 @@ try-except:
10021002
def get_person_email(sensor: Sensor) -> str | None:
10031003
try:
10041004
return sensor.machine.line.department.engineer.emails[0]
1005-
except AttributeError, IndexError:
1005+
except AttributeError, TypeError:
10061006
return None
10071007
10081008
While this will likely work, it does not provide nearly the same level

0 commit comments

Comments
 (0)