From a4ae953dca38da768bcd1786aeba84bada32efb4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 23 Jun 2022 16:56:20 -0400 Subject: [PATCH 1/3] Add xfail test capturing new expectation. --- tests/fixtures.py | 16 ++++++++++++++++ tests/test_main.py | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/fixtures.py b/tests/fixtures.py index 08a478ac..6d9a9d2b 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -5,6 +5,7 @@ import pathlib import tempfile import textwrap +import functools import contextlib from .py39compat import FS_NONASCII @@ -294,3 +295,18 @@ def setUp(self): # Add self.zip_name to the front of sys.path. self.resources = contextlib.ExitStack() self.addCleanup(self.resources.close) + + +def parameterize(*args_set): + """Run test method with a series of parameters.""" + + def wrapper(func): + @functools.wraps(func) + def _inner(self): + for args in args_set: + with self.subTest(**args): + func(self, **args) + + return _inner + + return wrapper diff --git a/tests/test_main.py b/tests/test_main.py index 215662dd..041220f1 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,6 +1,7 @@ import re import json import pickle +import pytest import unittest import warnings import importlib @@ -50,6 +51,15 @@ def test_new_style_classes(self): self.assertIsInstance(Distribution, type) self.assertIsInstance(MetadataPathFinder, type) + @pytest.mark.xfail(reason="Not implemented") + @fixtures.parameterize( + dict(name=None), + dict(name=''), + ) + def test_invalid_inputs_to_from_name(self, name): + with self.assertRaises(Exception): + Distribution.from_name(name) + class ImportTests(fixtures.DistInfoPkg, unittest.TestCase): def test_import_nonexistent_module(self): From eb19c647519c754dd93b42a0c421101af73cf7a4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 24 Jun 2022 21:15:31 -0400 Subject: [PATCH 2/3] In Distribution.from_name, require a non-empty string. Fixes python/cpython#93259. --- importlib_metadata/__init__.py | 5 ++++- tests/test_main.py | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/importlib_metadata/__init__.py b/importlib_metadata/__init__.py index 29ce1175..bb1105b6 100644 --- a/importlib_metadata/__init__.py +++ b/importlib_metadata/__init__.py @@ -548,7 +548,7 @@ def locate_file(self, path): """ @classmethod - def from_name(cls, name): + def from_name(cls, name: str): """Return the Distribution for the given package name. :param name: The name of the distribution package to search for. @@ -556,7 +556,10 @@ def from_name(cls, name): package, if found. :raises PackageNotFoundError: When the named package's distribution metadata cannot be found. + :raises ValueError: When an invalid value is supplied for name. """ + if not name: + raise ValueError("A distribution name is required.") for resolver in cls._discover_resolvers(): dists = resolver(DistributionFinder.Context(name=name)) dist = next(iter(dists), None) diff --git a/tests/test_main.py b/tests/test_main.py index 041220f1..921f5d9c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,7 +1,6 @@ import re import json import pickle -import pytest import unittest import warnings import importlib @@ -51,7 +50,6 @@ def test_new_style_classes(self): self.assertIsInstance(Distribution, type) self.assertIsInstance(MetadataPathFinder, type) - @pytest.mark.xfail(reason="Not implemented") @fixtures.parameterize( dict(name=None), dict(name=''), From 91b71494226a95251134c4fe6ea65a1dd25f495c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 25 Jun 2022 11:53:00 -0400 Subject: [PATCH 3/3] Update changelog --- CHANGES.rst | 7 +++++++ docs/conf.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index bf7098cc..84684eec 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,10 @@ +v4.12.0 +======= + +* py-93259: Now raise ``ValueError`` when ``None`` or an empty + string are passed to ``Distribution.from_name`` (and other + callers). + v4.11.4 ======= diff --git a/docs/conf.py b/docs/conf.py index 0192ca80..ec2bfe59 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,6 +20,10 @@ pattern=r'PEP[- ](?P\d+)', url='https://peps.python.org/pep-{pep_number:0>4}/', ), + dict( + pattern=r'(Python #|py-)(?P\d+)', + url='https://github.com/python/cpython/issues/{python}', + ), ], ) }