Skip to content

Commit bf69e16

Browse files
bpo-38010 Sync importlib.metadata with importlib_metadata 0.20. (GH-15646) (GH-15648)
Sync importlib.metadata with importlib_metadata 0.20. (cherry picked from commit 102e9b4) Co-authored-by: Jason R. Coombs <[email protected]>
1 parent 353053d commit bf69e16

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

Doc/library/importlib.metadata.rst

+7
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ Once you have the file, you can also read its contents::
158158
return s.encode('utf-8')
159159
return s
160160

161+
In the case where the metadata file listing files
162+
(RECORD or SOURCES.txt) is missing, ``files()`` will
163+
return ``None``. The caller may wish to wrap calls to
164+
``files()`` in `always_iterable
165+
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_
166+
or otherwise guard against this condition if the target
167+
distribution is not known to have the metadata present.
161168

162169
.. _requirements:
163170

Lib/importlib/metadata.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ def entry_points(self):
213213

214214
@property
215215
def files(self):
216+
"""Files in this distribution.
217+
218+
:return: Iterable of PackagePath for this distribution or None
219+
220+
Result is `None` if the metadata file that enumerates files
221+
(i.e. RECORD for dist-info or SOURCES.txt for egg-info) is
222+
missing.
223+
Result may be empty if the metadata exists but is empty.
224+
"""
216225
file_lines = self._read_files_distinfo() or self._read_files_egginfo()
217226

218227
def make_file(name, hash=None, size_str=None):
@@ -245,8 +254,7 @@ def requires(self):
245254
return self._read_dist_info_reqs() or self._read_egg_info_reqs()
246255

247256
def _read_dist_info_reqs(self):
248-
spec = self.metadata['Requires-Dist']
249-
return spec and filter(None, spec.splitlines())
257+
return self.metadata.get_all('Requires-Dist')
250258

251259
def _read_egg_info_reqs(self):
252260
source = self.read_text('requires.txt')
@@ -318,7 +326,11 @@ def find_distributions(self, name=None, path=None):
318326

319327
class PathDistribution(Distribution):
320328
def __init__(self, path):
321-
"""Construct a distribution from a path to the metadata directory."""
329+
"""Construct a distribution from a path to the metadata directory.
330+
331+
:param path: A pathlib.Path or similar object supporting
332+
.joinpath(), __div__, .parent, and .read_text().
333+
"""
322334
self._path = path
323335

324336
def read_text(self, filename):

Lib/test/test_importlib/test_metadata_api.py

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def test_requires(self):
109109
def test_requires_dist_info(self):
110110
deps = list(requires('distinfo-pkg'))
111111
assert deps and all(deps)
112+
assert 'wheel >= 1.0' in deps
113+
assert "pytest; extra == 'test'" in deps
112114

113115
def test_more_complex_deps_requires_text(self):
114116
requires = textwrap.dedent("""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
In ``importlib.metadata`` sync with ``importlib_metadata`` 0.20, clarifying behavior of ``files()`` and fixing issue where only one requirement was returned for ``requires()`` on ``dist-info`` packages.

0 commit comments

Comments
 (0)