From 2341ffc6eab2a38190810e0c8c5b47b0aee997a5 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Wed, 20 Mar 2024 14:15:43 -0500 Subject: [PATCH] Use importlib.metadata instead of pkg_resources for entrypoint processing --- setup.py | 4 +++- src/pgwui_develop/pytest_plugin_helpers.py | 17 ++++++++++++---- tests/test_pytest_plugin_helpers.py | 23 +++++++++++----------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index f54b674..e358bf6 100644 --- a/setup.py +++ b/setup.py @@ -132,9 +132,11 @@ setup( # Run-time dependencies. install_requires=[ 'click', + # for pytest_plugin_helpers.py's entrypoint processing. + 'importlib_metadata > 3.6 ; python_version < "3.10"', + # for pgwui.py's use of files() without an argument. 'importlib_resources >= 5.10 ; python_version < "3.12"', 'mako', - 'setuptools', # for pkg_resources module 'pyramid', 'WebTest >= 1.3.1', # py3 compat 'pytest>=3.7.4', diff --git a/src/pgwui_develop/pytest_plugin_helpers.py b/src/pgwui_develop/pytest_plugin_helpers.py index 21a4293..4ee8c1a 100644 --- a/src/pgwui_develop/pytest_plugin_helpers.py +++ b/src/pgwui_develop/pytest_plugin_helpers.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015, 2018, 2020 The Meme Factory, Inc. +# Copyright (C) 2015, 2018, 2020, 2024 The Meme Factory, Inc. # http://www.karlpinc.com/ # This file is part of PGWUI_Develop. @@ -23,7 +23,16 @@ '''Functions used by the pytest_plugin module. ''' -import pkg_resources +import sys + +if sys.version_info < (3, 10): + import importlib_metadata + + class importlib: + pass + setattr(importlib, 'metadata', importlib_metadata) +else: + import importlib.metadata def pgwui_entry_point(ep_name): @@ -32,7 +41,7 @@ def pgwui_entry_point(ep_name): ''' def run(module): return (module in - [entry_point.module_name for entry_point in - pkg_resources.iter_entry_points(ep_name)]) + [entry_point.name for entry_point in + importlib.metadata.entry_points(group=ep_name)]) return run diff --git a/tests/test_pytest_plugin_helpers.py b/tests/test_pytest_plugin_helpers.py index a7dabfc..b1921b8 100644 --- a/tests/test_pytest_plugin_helpers.py +++ b/tests/test_pytest_plugin_helpers.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020, 2021 The Meme Factory, Inc. +# Copyright (C) 2020, 2021, 2024 The Meme Factory, Inc. # http://www.karlpinc.com/ # This file is part of PGWUI_Develop. @@ -37,15 +37,15 @@ def test_pgwui_entry_point_there(monkeypatch): class MockEntryPoint(): def __init__(self, module_name): - self.module_name = module_name + self.name = module_name monkeypatch.setattr( - pytest_plugin_helpers.pkg_resources, 'iter_entry_points', - lambda *args: [MockEntryPoint(test_name)]) + pytest_plugin_helpers.importlib.metadata, 'entry_points', + lambda *args, **kwargs: [MockEntryPoint(test_name)]) - assert ( - pytest_plugin_helpers.pgwui_entry_point(test_name)(test_name) - is True) + result = pytest_plugin_helpers.pgwui_entry_point(test_name)(test_name) + + assert result is True @pytest.mark.unittest @@ -53,8 +53,9 @@ def test_pgwui_entry_point_not_there(monkeypatch): '''False when the component is not pgwui.components entry point ''' monkeypatch.setattr( - pytest_plugin_helpers.pkg_resources, 'iter_entry_points', - lambda *args: []) + pytest_plugin_helpers.importlib.metadata, 'entry_points', + lambda *args, **kwargs: []) + + result = pytest_plugin_helpers.pgwui_entry_point('foo')('foo') - assert (pytest_plugin_helpers.pgwui_entry_point('foo')('foo') - is False) + assert result is False -- 2.34.1