From ff71ab62909d3d4305f16f20933173a579f7ebbc Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Tue, 30 Apr 2024 10:16:53 +0100 Subject: [PATCH 1/5] Add test capturing expectation about importlib.metadata --- .../backend/_test_boostrap-0.0.1.dist-info/METADATA | 2 ++ .../_test_boostrap-0.0.1.dist-info/entry_points.txt | 2 ++ .../pkg_intree_metadata/backend/intree_backend.py | 7 +++++++ tests/samples/pkg_intree_metadata/pyproject.toml | 3 +++ tests/test_inplace_hooks.py | 11 +++++++++++ 5 files changed, 25 insertions(+) create mode 100644 tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/METADATA create mode 100644 tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/entry_points.txt create mode 100644 tests/samples/pkg_intree_metadata/backend/intree_backend.py create mode 100644 tests/samples/pkg_intree_metadata/pyproject.toml diff --git a/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/METADATA b/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/METADATA new file mode 100644 index 0000000..aaf174b --- /dev/null +++ b/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/METADATA @@ -0,0 +1,2 @@ +Name: _test_bootstrap +Version: 0.0.1 diff --git a/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/entry_points.txt b/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/entry_points.txt new file mode 100644 index 0000000..d62a9c9 --- /dev/null +++ b/tests/samples/pkg_intree_metadata/backend/_test_boostrap-0.0.1.dist-info/entry_points.txt @@ -0,0 +1,2 @@ +[_test_backend.importlib_metadata] +hello = world diff --git a/tests/samples/pkg_intree_metadata/backend/intree_backend.py b/tests/samples/pkg_intree_metadata/backend/intree_backend.py new file mode 100644 index 0000000..9748082 --- /dev/null +++ b/tests/samples/pkg_intree_metadata/backend/intree_backend.py @@ -0,0 +1,7 @@ +from importlib.metadata import distribution + + +def get_requires_for_build_sdist(config_settings): + dist = distribution("_test_bootstrap") # discovered in backend-path + ep = next(iter(dist.entry_points)) + return [ep.group, ep.name, ep.value] diff --git a/tests/samples/pkg_intree_metadata/pyproject.toml b/tests/samples/pkg_intree_metadata/pyproject.toml new file mode 100644 index 0000000..fd4e8cb --- /dev/null +++ b/tests/samples/pkg_intree_metadata/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +build-backend = 'intree_backend' +backend-path = ['backend'] diff --git a/tests/test_inplace_hooks.py b/tests/test_inplace_hooks.py index 6b73ceb..f6fd71b 100644 --- a/tests/test_inplace_hooks.py +++ b/tests/test_inplace_hooks.py @@ -1,3 +1,4 @@ +import sys from inspect import cleandoc from os.path import abspath, dirname from os.path import join as pjoin @@ -89,6 +90,16 @@ def test_intree_backend_loaded_from_correct_backend_path(): assert res == ["intree_backend_called"] +@pytest.mark.skipif(sys.version_info < (3, 8), reason="no importlib.metadata") +def test_intree_backend_importlib_metadata_interoperation(): + hooks = get_hooks("pkg_intree_metadata", backend="intree_backend") + assert hooks.get_requires_for_build_sdist({}) == [ + "_test_backend.importlib_metadata", + "hello", + "world", + ] + + def install_finder_with_sitecustomize(directory, mapping): finder = f""" import sys From 282b8735e59df938a210e46b8cd5cd1d99344816 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Tue, 30 Apr 2024 10:21:07 +0100 Subject: [PATCH 2/5] Implement find_distributions for _BackendPathFinder --- src/pyproject_hooks/_in_process/_in_process.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pyproject_hooks/_in_process/_in_process.py b/src/pyproject_hooks/_in_process/_in_process.py index 0645b0c..0b49c35 100644 --- a/src/pyproject_hooks/_in_process/_in_process.py +++ b/src/pyproject_hooks/_in_process/_in_process.py @@ -106,6 +106,13 @@ def find_spec(self, fullname, _path, _target=None): return spec + def find_distributions(self, context=None): + # Delayed import: Python 3.7 does not contain importlib.metadata + from importlib.metadata import DistributionFinder, MetadataPathFinder + + context = DistributionFinder.Context(path=self.backend_path) + return MetadataPathFinder.find_distributions(context=context) + def _supported_features(): """Return the list of options features supported by the backend. From ca5c5a522720a9c7b672a5a215fbfe6fa05caf77 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 1 May 2024 16:21:52 +0100 Subject: [PATCH 3/5] Use pytest.importorskip for importlib.metadata --- tests/test_inplace_hooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_inplace_hooks.py b/tests/test_inplace_hooks.py index f6fd71b..1df12d6 100644 --- a/tests/test_inplace_hooks.py +++ b/tests/test_inplace_hooks.py @@ -1,4 +1,3 @@ -import sys from inspect import cleandoc from os.path import abspath, dirname from os.path import join as pjoin @@ -90,8 +89,9 @@ def test_intree_backend_loaded_from_correct_backend_path(): assert res == ["intree_backend_called"] -@pytest.mark.skipif(sys.version_info < (3, 8), reason="no importlib.metadata") def test_intree_backend_importlib_metadata_interoperation(): + pytest.importorskip("importlib.metadata") + hooks = get_hooks("pkg_intree_metadata", backend="intree_backend") assert hooks.get_requires_for_build_sdist({}) == [ "_test_backend.importlib_metadata", From 455b77f9a89f3bfded5925ccc15e17d20c9b99cd Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 1 May 2024 16:27:31 +0100 Subject: [PATCH 4/5] Account for importlib_metadata in Python<3.7 --- dev-requirements.txt | 1 + src/pyproject_hooks/_in_process/_in_process.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 4a8c47a..69b161f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,3 +3,4 @@ flake8 testpath setuptools>=30 tomli >=1.1.0 ; python_version<'3.11' +importlib-metadata; python_version<'3.8' diff --git a/src/pyproject_hooks/_in_process/_in_process.py b/src/pyproject_hooks/_in_process/_in_process.py index 0b49c35..4f0bc24 100644 --- a/src/pyproject_hooks/_in_process/_in_process.py +++ b/src/pyproject_hooks/_in_process/_in_process.py @@ -108,7 +108,12 @@ def find_spec(self, fullname, _path, _target=None): def find_distributions(self, context=None): # Delayed import: Python 3.7 does not contain importlib.metadata - from importlib.metadata import DistributionFinder, MetadataPathFinder + # If this method is being called it must be because + # `importlib.metadata`/`importlib_metadata` is available. + try: + from importlib_metadata import DistributionFinder, MetadataPathFinder + except ImportError: + from importlib.metadata import DistributionFinder, MetadataPathFinder context = DistributionFinder.Context(path=self.backend_path) return MetadataPathFinder.find_distributions(context=context) From d68f4d94477f0c20f900b1520ef1a1193e9d4f9d Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 1 May 2024 16:35:40 +0100 Subject: [PATCH 5/5] Remove unused dev-dependency --- dev-requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 69b161f..4a8c47a 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,4 +3,3 @@ flake8 testpath setuptools>=30 tomli >=1.1.0 ; python_version<'3.11' -importlib-metadata; python_version<'3.8'