[Python-checkins] bpo-38010 Sync importlib.metadata with importlib_metadata 0.20. (GH-15646)

Jason R. Coombs webhook-mailer at python.org
Mon Sep 2 11:08:07 EDT 2019


https://github.com/python/cpython/commit/102e9b40ff6ee45086a5f0d34d9c60c581a1e5e5
commit: 102e9b40ff6ee45086a5f0d34d9c60c581a1e5e5
branch: master
author: Jason R. Coombs <jaraco at jaraco.com>
committer: GitHub <noreply at github.com>
date: 2019-09-02T11:08:03-04:00
summary:

bpo-38010 Sync importlib.metadata with importlib_metadata 0.20. (GH-15646)

Sync importlib.metadata with importlib_metadata 0.20.

files:
A Misc/NEWS.d/next/Library/2019-09-02-14-30-39.bpo-38010.JOnz9Z.rst
M Doc/library/importlib.metadata.rst
M Lib/importlib/metadata.py
M Lib/test/test_importlib/test_metadata_api.py

diff --git a/Doc/library/importlib.metadata.rst b/Doc/library/importlib.metadata.rst
index 2126498e728c..23c8cdeb21c2 100644
--- a/Doc/library/importlib.metadata.rst
+++ b/Doc/library/importlib.metadata.rst
@@ -158,6 +158,13 @@ Once you have the file, you can also read its contents::
             return s.encode('utf-8')
         return s
 
+In the case where the metadata file listing files
+(RECORD or SOURCES.txt) is missing, ``files()`` will
+return ``None``. The caller may wish to wrap calls to
+``files()`` in `always_iterable
+<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_
+or otherwise guard against this condition if the target
+distribution is not known to have the metadata present.
 
 .. _requirements:
 
diff --git a/Lib/importlib/metadata.py b/Lib/importlib/metadata.py
index e80f4fa6409b..3b46142231ec 100644
--- a/Lib/importlib/metadata.py
+++ b/Lib/importlib/metadata.py
@@ -213,6 +213,15 @@ def entry_points(self):
 
     @property
     def files(self):
+        """Files in this distribution.
+
+        :return: Iterable of PackagePath for this distribution or None
+
+        Result is `None` if the metadata file that enumerates files
+        (i.e. RECORD for dist-info or SOURCES.txt for egg-info) is
+        missing.
+        Result may be empty if the metadata exists but is empty.
+        """
         file_lines = self._read_files_distinfo() or self._read_files_egginfo()
 
         def make_file(name, hash=None, size_str=None):
@@ -245,8 +254,7 @@ def requires(self):
         return self._read_dist_info_reqs() or self._read_egg_info_reqs()
 
     def _read_dist_info_reqs(self):
-        spec = self.metadata['Requires-Dist']
-        return spec and filter(None, spec.splitlines())
+        return self.metadata.get_all('Requires-Dist')
 
     def _read_egg_info_reqs(self):
         source = self.read_text('requires.txt')
@@ -318,7 +326,11 @@ def find_distributions(self, name=None, path=None):
 
 class PathDistribution(Distribution):
     def __init__(self, path):
-        """Construct a distribution from a path to the metadata directory."""
+        """Construct a distribution from a path to the metadata directory.
+
+        :param path: A pathlib.Path or similar object supporting
+                     .joinpath(), __div__, .parent, and .read_text().
+        """
         self._path = path
 
     def read_text(self, filename):
diff --git a/Lib/test/test_importlib/test_metadata_api.py b/Lib/test/test_importlib/test_metadata_api.py
index 899777f4b1ad..af3bab3558ef 100644
--- a/Lib/test/test_importlib/test_metadata_api.py
+++ b/Lib/test/test_importlib/test_metadata_api.py
@@ -109,6 +109,8 @@ def test_requires(self):
     def test_requires_dist_info(self):
         deps = list(requires('distinfo-pkg'))
         assert deps and all(deps)
+        assert 'wheel >= 1.0' in deps
+        assert "pytest; extra == 'test'" in deps
 
     def test_more_complex_deps_requires_text(self):
         requires = textwrap.dedent("""
diff --git a/Misc/NEWS.d/next/Library/2019-09-02-14-30-39.bpo-38010.JOnz9Z.rst b/Misc/NEWS.d/next/Library/2019-09-02-14-30-39.bpo-38010.JOnz9Z.rst
new file mode 100644
index 000000000000..a97f9b7d9a40
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-02-14-30-39.bpo-38010.JOnz9Z.rst
@@ -0,0 +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.



More information about the Python-checkins mailing list