Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFE: Replace use of pkg_resources with importlib.metadata #9884

Closed
kloczek opened this issue Apr 23, 2022 · 6 comments
Closed

RFE: Replace use of pkg_resources with importlib.metadata #9884

kloczek opened this issue Apr 23, 2022 · 6 comments

Comments

@kloczek
Copy link

kloczek commented Apr 23, 2022

See discussions:
astropy/astropy#11091
pypa/pip#7413

[tkloczko@devel-g2v pytest-7.1.2]$ grep -r pkg_resources
.pre-commit-config.yaml:          - types-pkg_resources
doc/en/announce/release-2.5.0.rst:- refine pytest / pkg_resources interactions: The AssertionRewritingHook
doc/en/announce/release-2.5.0.rst:  PEP302 compliant loader now registers itself with setuptools/pkg_resources
doc/en/announce/release-2.5.0.rst:  properly so that the pkg_resources.resource_stream method works properly.
doc/en/changelog.rst:  It now only calls :func:`pkg_resources.fixup_namespace_packages` if
doc/en/changelog.rst:  ``pkg_resources`` was previously imported, because it is not needed otherwise.
doc/en/changelog.rst:- :issue:`5063`: Switch from ``pkg_resources`` to ``importlib-metadata`` for entrypoint detection for improved performance and import time.
doc/en/changelog.rst:- :issue:`4980`: Namespace packages are handled better with ``monkeypatch.syspath_prepend`` and ``testdir.syspathinsert`` (via ``pkg_resources.fixup_namespace_packages``).
doc/en/changelog.rst:- :issue:`4768`: Avoid pkg_resources import at the top-level.
doc/en/changelog.rst:- :issue:`4315`: Use ``pkg_resources.parse_version`` instead of ``LooseVersion`` in minversion check.
doc/en/changelog.rst:* Fix pkg_resources import error in Jython projects (:issue:`1853`).
doc/en/changelog.rst:- refine pytest / pkg_resources interactions: The AssertionRewritingHook
doc/en/changelog.rst:  :pep:`302` compliant loader now registers itself with setuptools/pkg_resources
doc/en/changelog.rst:  properly so that the pkg_resources.resource_stream method works properly.
doc/en/changelog.rst:  entrypoints via setuptools' pkg_resources.iter_entry_points
doc/en/how-to/monkeypatch.rst:call ``pkg_resources.fixup_namespace_packages`` and :py:func:`importlib.invalidate_caches`.
src/_pytest/monkeypatch.py:        # https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
src/_pytest/monkeypatch.py:        # this is only needed when pkg_resources was already loaded by the namespace package
src/_pytest/monkeypatch.py:        if "pkg_resources" in sys.modules:
src/_pytest/monkeypatch.py:            from pkg_resources import fixup_namespace_packages
grep: src/_pytest/__pycache__/monkeypatch.cpython-38.pyc: binary file matches
testing/acceptance_test.py:                "__import__('pkg_resources').declare_namespace(__name__)"
testing/test_assertrewrite.py:        pytest.importorskip("pkg_resources")
testing/test_assertrewrite.py:                import pkg_resources
testing/test_assertrewrite.py:                    res = pkg_resources.resource_string(__name__, 'resource.txt')
testing/test_monkeypatch.py:            "__import__('pkg_resources').declare_namespace(__name__)"
@The-Compiler
Copy link
Member

As you can see in the changelog you quoted, that has already happened.

@kloczek
Copy link
Author

kloczek commented Apr 23, 2022

I see as well lines where that module still is used despite those changelog entries .. 😄

@The-Compiler
Copy link
Member

Did you try looking at the code in question?

@kloczek
Copy link
Author

kloczek commented Apr 23, 2022

No, just checked grep outup as I saw not commented lines with imports I made assumption that still it is something to do .. 🤔

@nicoddemus
Copy link
Member

nicoddemus commented Apr 24, 2022

Here's the code in question:

# https://github.com/pypa/setuptools/blob/d8b901bc/docs/pkg_resources.txt#L162-L171
# 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))

pytest itself does not import pkg_resources, but if already imported, then it imports it to call fixup_namespace_packages(), which is the only thing done with pkg_resources. That's what @The-Compiler means.

For everything else pytest already uses importlib.metadata. 👍

@kloczek
Copy link
Author

kloczek commented Apr 24, 2022

OK, thank you 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants