From 778d2b2499e78759997b3e717f1f07a344288c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 1 Apr 2021 17:21:45 +0200 Subject: [PATCH] monkeypatch.syspath_prepend: Skip fixup_namespace_packages if pkg_resources not imported Calling pkg_resources.fixup_namespace_packages() is only needed for packages that use pkg_resources.declare_namespace() and hence they already imported pkg_resources. When pkg_resources is not imported, we don't need to use it. This avoids an unneeded runtime dependency on setuptools. The code is tested by test_syspath_prepend_with_namespace_packages, behavior should remain unchanged, hence no new test was added. When people drop pkg_resources from sys.modules, they are on their own. If someone has a actual use case making this valid to support, they can come in and provide a test, a reference and a fix. --- changelog/8503.trivial.rst | 4 ++++ src/_pytest/monkeypatch.py | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelog/8503.trivial.rst diff --git a/changelog/8503.trivial.rst b/changelog/8503.trivial.rst new file mode 100644 index 00000000000..26f660bbf3f --- /dev/null +++ b/changelog/8503.trivial.rst @@ -0,0 +1,4 @@ +:meth:`pytest.MonkeyPatch.syspath_prepend` no longer fails when +``setuptools`` is not installed. +It now only calls :func:`pkg_resources.fixup_namespace_packages` if +``pkg_resources`` was previously imported, because it is not needed otherwise. diff --git a/src/_pytest/monkeypatch.py b/src/_pytest/monkeypatch.py index 2c432065625..708b25aa073 100644 --- a/src/_pytest/monkeypatch.py +++ b/src/_pytest/monkeypatch.py @@ -312,14 +312,17 @@ def delenv(self, name: str, raising: bool = True) -> None: def syspath_prepend(self, path) -> None: """Prepend ``path`` to ``sys.path`` list of import locations.""" - from pkg_resources import fixup_namespace_packages if self._savesyspath is None: self._savesyspath = sys.path[:] sys.path.insert(0, str(path)) # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171 - fixup_namespace_packages(str(path)) + # this is only needed when pkg_resources was already loaded by the namespace package + if "pkg_resources" in sys.modules: + from pkg_resources import fixup_namespace_packages + + fixup_namespace_packages(str(path)) # A call to syspathinsert() usually means that the caller wants to # import some dynamically created files, thus with python3 we