Closed
Description
https://setuptools.pypa.io/en/stable/history.html#v67-3-0
#3434: Added deprecation warning for pkg_resources.declare_namespace. Users that wish to implement namespace packages, are recommended to follow the practice described in PEP 420 and omit the init.py file entirely.
This causes 2 failures in Pytest's test suite.
Pytest version: 7.2.0 and current main.
test logs
=================================== FAILURES ===================================
_________ TestInvocationVariants.test_cmdline_python_namespace_package _________
self = <acceptance_test.TestInvocationVariants object at 0x7fe9a34638e0>
pytester = <Pytester PosixPath('/usr/src/tmp/pytest-of-builder/pytest-0/test_cmdline_p
ython_namespace_package0')>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7fe99b6a2ad0>
def test_cmdline_python_namespace_package(
self, pytester: Pytester, monkeypatch
) -> None:
"""Test --pyargs option with namespace packages (#1567).
Ref: https://packaging.python.org/guides/packaging-namespace-packages/
"""
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
search_path = []
for dirname in "hello", "world":
d = pytester.mkdir(dirname)
search_path.append(d)
ns = d.joinpath("ns_pkg")
ns.mkdir()
ns.joinpath("__init__.py").write_text(
"__import__('pkg_resources').declare_namespace(__name__)"
)
lib = ns.joinpath(dirname)
lib.mkdir()
lib.joinpath("__init__.py").touch()
lib.joinpath(f"test_{dirname}.py").write_text(
f"def test_{dirname}(): pass\ndef test_other():pass"
)
# The structure of the test directory is now:
# .
# ├── hello
# │ └── ns_pkg
# │ ├── __init__.py
# │ └── hello
# │ ├── __init__.py
# │ └── test_hello.py
# └── world
# └── ns_pkg
# ├── __init__.py
# └── world
# ├── __init__.py
# └── test_world.py
# NOTE: the different/reversed ordering is intentional here.
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))
for p in search_path:
monkeypatch.syspath_prepend(p)
# mixed module and filenames:
monkeypatch.chdir("world")
result = pytester.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")
assert result.ret == 0
> result.stdout.fnmatch_lines(
[
"test_hello.py::test_hello*PASSED*",
"test_hello.py::test_other*PASSED*",
"ns_pkg/world/test_world.py::test_world*PASSED*",
"ns_pkg/world/test_world.py::test_other*PASSED*",
"*4 passed in*",
]
)
E Failed: nomatch: 'test_hello.py::test_hello*PASSED*'
E and: '============================= test session starts =============================='
E and: 'platform linux -- Python 3.10.8, pytest-7.2.1, pluggy-1.0.0 -- /usr/src/RPM/BUILD/python3-module-pytest-7.2.1/.run_venv/bin/python3'
E and: 'cachedir: .pytest_cache'
E and: 'rootdir: /usr/src/tmp/pytest-of-builder/pytest-0/test_cmdline_python_namespace_package0/world'
E and: 'collecting ... collected 4 items'
E and: ''
E fnmatch: 'test_hello.py::test_hello*PASSED*'
E with: 'test_hello.py::test_hello PASSED [ 25%]'
E fnmatch: 'test_hello.py::test_other*PASSED*'
E with: 'test_hello.py::test_other PASSED [ 50%]'
E fnmatch: 'ns_pkg/world/test_world.py::test_world*PASSED*'
E with: 'ns_pkg/world/test_world.py::test_world PASSED [ 75%]'
E fnmatch: 'ns_pkg/world/test_world.py::test_other*PASSED*'
E with: 'ns_pkg/world/test_world.py::test_other PASSED [100%]'
E nomatch: '*4 passed in*'
E and: ''
E and: '=============================== warnings summary ==============================='
E and: '../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804'
E and: " /usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`."
E and: ' Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages'
E and: ' declare_namespace(pkg)'
E and: ''
E and: '../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804'
E and: '../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804'
E and: " /usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('zope')`."
E and: ' Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages'
E and: ' declare_namespace(pkg)'
E and: ''
E and: '../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804'
E and: '../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804'
E and: " /usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('paste')`."
E and: ' Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages'
E and: ' declare_namespace(pkg)'
E and: ''
E and: 'ns_pkg/__init__.py:1'
E and: " /usr/src/tmp/pytest-of-builder/pytest-0/test_cmdline_python_namesp
ace_package0/world/ns_pkg/__init__.py:1: DeprecationWarning: Deprecated call to `pkg_r
esources.declare_namespace('ns_pkg')`."
E and: ' Implementing implicit namespace packages (as specified in PEP 420)
is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/
latest/references/keywords.html#keyword-namespace-packages'
E and: " __import__('pkg_resources').declare_namespace(__name__)"
E and: ''
E and: '-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.h
tml'
E and: '======================== 4 passed, 6 warnings in 0.23s =============
============'
E remains unmatched: '*4 passed in*'
/usr/src/RPM/BUILD/python3-module-pytest-7.2.1/testing/acceptance_test.py:699: Failed
----------------------------- Captured stdout call -----------------------------
============================= test session starts ==============================
platform linux -- Python 3.10.8, pytest-7.2.1, pluggy-1.0.0 -- /usr/src/RPM/BUILD/pyth
on3-module-pytest-7.2.1/.run_venv/bin/python3
cachedir: .pytest_cache
rootdir: /usr/src/tmp/pytest-of-builder/pytest-0/test_cmdline_python_namespace_package
0/world
collecting ... collected 4 items
test_hello.py::test_hello PASSED [ 25%]
test_hello.py::test_other PASSED [ 50%]
ns_pkg/world/test_world.py::test_world PASSED [ 75%]
ns_pkg/world/test_world.py::test_other PASSED [100%]
=============================== warnings summary ===============================
../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning:
Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
declare_namespace(pkg)
../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804
../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('zope')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
declare_namespace(pkg)
../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804
../../../../../../lib64/python3/site-packages/pkg_resources/__init__.py:2804
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('paste')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
declare_namespace(pkg)
ns_pkg/__init__.py:1
/usr/src/tmp/pytest-of-builder/pytest-0/test_cmdline_python_namespace_package0/world/ns_pkg/__init__.py:1: DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('ns_pkg')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
__import__('pkg_resources').declare_namespace(__name__)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
======================== 4 passed, 6 warnings in 0.23s =========================
_________________ test_syspath_prepend_with_namespace_packages _________________
pytester = <Pytester PosixPath('/usr/src/tmp/pytest-of-builder/pytest-0/test_syspath_prepend_with_namespace_packages0')>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7fe9a2b2d1e0>
def test_syspath_prepend_with_namespace_packages(
pytester: Pytester, monkeypatch: MonkeyPatch
) -> None:
for dirname in "hello", "world":
d = pytester.mkdir(dirname)
ns = d.joinpath("ns_pkg")
ns.mkdir()
ns.joinpath("__init__.py").write_text(
"__import__('pkg_resources').declare_namespace(__name__)"
)
lib = ns.joinpath(dirname)
lib.mkdir()
lib.joinpath("__init__.py").write_text("def check(): return %r" % dirname)
monkeypatch.syspath_prepend("hello")
> import ns_pkg.hello
/usr/src/RPM/BUILD/python3-module-pytest-7.2.1/testing/test_monkeypatch.py:443:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/src/tmp/pytest-of-builder/pytest-0/test_syspath_prepend_with_namespace_packages0/hello/ns_pkg/__init__.py:1: in <module>
__import__('pkg_resources').declare_namespace(__name__)
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:3258: in <module>
def _initialize_master_working_set():
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:3232: in _call_aside
f(*args, **kwargs)
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:3283: in _initialize_master_working_set
tuple(dist.activate(replace=False) for dist in working_set)
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:3283: in <genexpr>
tuple(dist.activate(replace=False) for dist in working_set)
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:2804: in activate
declare_namespace(pkg)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
packageName = 'mpl_toolkits'
def declare_namespace(packageName):
"""Declare that package 'packageName' is a namespace package"""
msg = (
f"Deprecated call to `pkg_resources.declare_namespace({packageName!r})`.\n"
"Implementing implicit namespace packages (as specified in PEP 420) "
"is preferred to `pkg_resources.declare_namespace`. "
"See https://setuptools.pypa.io/en/latest/references/"
"keywords.html#keyword-namespace-packages"
)
> warnings.warn(msg, DeprecationWarning, stacklevel=2)
E DeprecationWarning: Deprecated call to `pkg_resources.declare_namespace('mpl_toolkits')`.
E Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
/usr/lib64/python3/site-packages/pkg_resources/__init__.py:2298: DeprecationWarning
=========================== short test summary info ============================