From 828198848bd5c91eae04441096e92c6b8376877b Mon Sep 17 00:00:00 2001 From: Facundo Batista Date: Sat, 15 Feb 2025 15:09:52 -0300 Subject: [PATCH 1/2] Better explanation on which exceptions are captured --- peps/pep-0769.rst | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/peps/pep-0769.rst b/peps/pep-0769.rst index 38b8f82ed83..35ac55b5554 100644 --- a/peps/pep-0769.rst +++ b/peps/pep-0769.rst @@ -189,7 +189,7 @@ The implementation of ``attrgetter`` is quite direct: it implies using try: value = getattr(obj, "name") - except (TypeError, IndexError, KeyError): + except (IndexError, KeyError): value = XYZ Note we cannot rely on using ``getattr`` with a default value, as it would @@ -200,28 +200,27 @@ attribute chain is specified (e.g. The implementation for ``itemgetter`` and ``getitem`` is not that easy. The more straightforward way is also simple to define and understand: attempting ``__getitem__`` and catching a possible -exception (any of the three indicated in ``__getitem__`` `reference`_). -This way, ``itemgetter(123, default=XYZ)(obj)`` or -``getitem(obj, 123, default=XYZ)`` would be equivalent to:: +exception (see below). This way, ``itemgetter(123, default=XYZ)(obj)`` +or ``getitem(obj, 123, default=XYZ)`` would be equivalent to:: try: value = obj[123] - except (TypeError, IndexError, KeyError): + except (IndexError, KeyError): value = XYZ However, for performance reasons the implementation may look more -like the following, which has the same exact behaviour:: +like the following, which has the same exact behavior:: if type(obj) == dict: value = obj.get(123, XYZ) else: try: value = obj[123] - except (TypeError, IndexError, KeyError): + except (IndexError, KeyError): value = XYZ Note how the verification is about the exact type and not using -``isinstance``; this is to ensure the exact behaviour, which would be +``isinstance``; this is to ensure the exact behavior, which would be impossible if the object is a user defined one that inherits ``dict`` but overwrites ``get`` (similar reason to not check if the object has a ``get`` method). @@ -229,6 +228,12 @@ a ``get`` method). This way, performance is better but it's just an implementation detail, so we can keep the original explanation on how it behaves. +Regarding the exception to be captured, even if ``__getitem__`` +`reference`_ can raise ``IndexError``, ``KeyError``, or ``TypeError``, +only the first two can happen if the container does not contain the +indicated key or index, and the latter is likely to signal a bug in the +code, so we're not capturing it to trigger the default behavior. + Corner Cases ------------ From a069ae2a5ef23aa450b60bef5d218f869e01421a Mon Sep 17 00:00:00 2001 From: Facundo Batista Date: Sat, 15 Feb 2025 15:19:13 -0300 Subject: [PATCH 2/2] Better wording --- peps/pep-0769.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/peps/pep-0769.rst b/peps/pep-0769.rst index 35ac55b5554..d51bf6e2009 100644 --- a/peps/pep-0769.rst +++ b/peps/pep-0769.rst @@ -229,10 +229,11 @@ This way, performance is better but it's just an implementation detail, so we can keep the original explanation on how it behaves. Regarding the exception to be captured, even if ``__getitem__`` -`reference`_ can raise ``IndexError``, ``KeyError``, or ``TypeError``, -only the first two can happen if the container does not contain the -indicated key or index, and the latter is likely to signal a bug in the -code, so we're not capturing it to trigger the default behavior. +can raise ``IndexError``, ``KeyError``, or ``TypeError`` (see its +`reference`_), only the first two can happen if the container does not +contain the indicated key or index, and the latter is likely to signal +a bug in the code, so we're not capturing it to trigger the default +behavior. Corner Cases