From a5955c1ee9f98b6f75e5f7f390766d20bf96251e Mon Sep 17 00:00:00 2001 From: bswck Date: Wed, 15 Oct 2025 02:10:34 +0200 Subject: [PATCH 1/3] Properly break exception chain in `importlib.metadata.Distribution.from_name` --- Lib/importlib/metadata/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 1e2cea4009482a..b010bb8525e5cc 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -457,7 +457,7 @@ def from_name(cls, name: str) -> Distribution: try: return next(iter(cls._prefer_valid(cls.discover(name=name)))) except StopIteration: - raise PackageNotFoundError(name) + raise PackageNotFoundError(name) from None @classmethod def discover( From 741efba10d0e3e80ca833f4e3a221a395593de1f Mon Sep 17 00:00:00 2001 From: bswck Date: Wed, 15 Oct 2025 17:24:26 +0200 Subject: [PATCH 2/3] Add news entry --- .../Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst diff --git a/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst b/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst new file mode 100644 index 00000000000000..90c5dd906fc033 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst @@ -0,0 +1,6 @@ +When a distribution cannot be discovered with +``importlib.metadata.Distribution.from_name``, the +:py:class:`importlib.metadata.PackageNotFoundError` traceback no longer +includes a transient :exc:`StopIteration` exception. + +Contributed by Bartosz Sławecki in :gh:`140142`. From 8a8323755c4745f3ec6d2c457d3513280fcbe486 Mon Sep 17 00:00:00 2001 From: bswck Date: Wed, 15 Oct 2025 17:26:57 +0200 Subject: [PATCH 3/3] Rephrase the news entry --- .../Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst b/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst index 90c5dd906fc033..2edadbc3e3887c 100644 --- a/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst +++ b/Misc/NEWS.d/next/Library/2025-10-15-17-23-51.gh-issue-140141.j2mUDB.rst @@ -1,6 +1,5 @@ -When a distribution cannot be discovered with -``importlib.metadata.Distribution.from_name``, the -:py:class:`importlib.metadata.PackageNotFoundError` traceback no longer -includes a transient :exc:`StopIteration` exception. +The :py:class:`importlib.metadata.PackageNotFoundError` traceback raised when +``importlib.metadata.Distribution.from_name`` cannot discover a +distribution no longer includes a transient :exc:`StopIteration` exception trace. Contributed by Bartosz Sławecki in :gh:`140142`.