Skip to content

Commit c8d7285

Browse files
authored
Merge pull request #391 from python/ghpython-93259/from-name-arg-validation-simple
Validate name arg in Distribution.from_name
2 parents f52757d + 91b7149 commit c8d7285

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

CHANGES.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
v4.12.0
2+
=======
3+
4+
* py-93259: Now raise ``ValueError`` when ``None`` or an empty
5+
string are passed to ``Distribution.from_name`` (and other
6+
callers).
7+
18
v4.11.4
29
=======
310

docs/conf.py

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
pattern=r'PEP[- ](?P<pep_number>\d+)',
2121
url='https://peps.python.org/pep-{pep_number:0>4}/',
2222
),
23+
dict(
24+
pattern=r'(Python #|py-)(?P<python>\d+)',
25+
url='https://github.com/python/cpython/issues/{python}',
26+
),
2327
],
2428
)
2529
}

importlib_metadata/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,18 @@ def locate_file(self, path):
548548
"""
549549

550550
@classmethod
551-
def from_name(cls, name):
551+
def from_name(cls, name: str):
552552
"""Return the Distribution for the given package name.
553553
554554
:param name: The name of the distribution package to search for.
555555
:return: The Distribution instance (or subclass thereof) for the named
556556
package, if found.
557557
:raises PackageNotFoundError: When the named package's distribution
558558
metadata cannot be found.
559+
:raises ValueError: When an invalid value is supplied for name.
559560
"""
561+
if not name:
562+
raise ValueError("A distribution name is required.")
560563
try:
561564
return next(cls.discover(name=name))
562565
except StopIteration:

tests/fixtures.py

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66
import tempfile
77
import textwrap
8+
import functools
89
import contextlib
910

1011
from .py39compat import FS_NONASCII
@@ -294,3 +295,18 @@ def setUp(self):
294295
# Add self.zip_name to the front of sys.path.
295296
self.resources = contextlib.ExitStack()
296297
self.addCleanup(self.resources.close)
298+
299+
300+
def parameterize(*args_set):
301+
"""Run test method with a series of parameters."""
302+
303+
def wrapper(func):
304+
@functools.wraps(func)
305+
def _inner(self):
306+
for args in args_set:
307+
with self.subTest(**args):
308+
func(self, **args)
309+
310+
return _inner
311+
312+
return wrapper

tests/test_main.py

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def test_new_style_classes(self):
5050
self.assertIsInstance(Distribution, type)
5151
self.assertIsInstance(MetadataPathFinder, type)
5252

53+
@fixtures.parameterize(
54+
dict(name=None),
55+
dict(name=''),
56+
)
57+
def test_invalid_inputs_to_from_name(self, name):
58+
with self.assertRaises(Exception):
59+
Distribution.from_name(name)
60+
5361

5462
class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
5563
def test_import_nonexistent_module(self):

0 commit comments

Comments
 (0)