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

core: replace pkg_resources with importlib.metadata #2261

Merged
merged 7 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ geoip2>=4.0,<5.0
requests>=2.24.0,<3.0.0
dnspython<3.0
sqlalchemy>=1.4,<1.5
importlib_metadata>=3.6; python_version < '3.10'
packaging
8 changes: 6 additions & 2 deletions sopel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import re
import sys

import pkg_resources
try:
import importlib.metadata as importlib_metadata
except ImportError:
# TODO: remove fallback when dropping py3.7
import importlib_metadata

__all__ = [
'bot',
Expand All @@ -41,7 +45,7 @@
'something like "en_US.UTF-8".', file=sys.stderr)


__version__ = pkg_resources.get_distribution('sopel').version
__version__ = importlib_metadata.version('sopel')


def _version_info(version=__version__):
Expand Down
2 changes: 1 addition & 1 deletion sopel/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import traceback
from typing import Callable, Optional

from pkg_resources import parse_version
from packaging.version import parse as parse_version

from sopel import __version__

Expand Down
9 changes: 7 additions & 2 deletions sopel/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
import itertools
import os

import pkg_resources
try:
import importlib_metadata
except ImportError:
# TODO: use stdlib only when possible, after dropping py3.9
# stdlib does not support `entry_points(group='filter')` until py3.10
Exirel marked this conversation as resolved.
Show resolved Hide resolved
import importlib.metadata as importlib_metadata

from . import exceptions, handlers, rules # noqa

Expand Down Expand Up @@ -103,7 +108,7 @@ def find_entry_point_plugins(group='sopel.plugins'):
This function finds plugins declared under a setuptools entry point; by
default it uses the ``sopel.plugins`` entry point.
"""
for entry_point in pkg_resources.iter_entry_points(group):
for entry_point in importlib_metadata.entry_points(group=group):
yield handlers.EntryPointPlugin(entry_point)


Expand Down
9 changes: 6 additions & 3 deletions sopel/plugins/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ class EntryPointPlugin(PyModulePlugin):

And this plugin can be loaded with::

>>> from pkg_resources import iter_entry_points
>>> from importlib_metadata import entry_points
Exirel marked this conversation as resolved.
Show resolved Hide resolved
>>> from sopel.plugins.handlers import EntryPointPlugin
>>> plugin = [
... EntryPointPlugin(ep)
... for ep in iter_entry_points('sopel.plugins', 'custom')
... for ep in entry_points(group='sopel.plugins', name='custom')
... ][0]
>>> plugin.load()
>>> plugin.name
Expand All @@ -559,6 +559,9 @@ class EntryPointPlugin(PyModulePlugin):
Entry point is a `standard feature of setuptools`__ for Python, used
by other applications (like ``pytest``) for their plugins.

The ``importlib_metadata`` backport package is used on Python versions
older than 3.10, but its API is the same as :mod:`importlib.metadata`.

.. __: https://setuptools.readthedocs.io/en/stable/setuptools.html#dynamic-discovery-of-services-and-plugins

"""
Expand Down Expand Up @@ -598,6 +601,6 @@ def get_meta_description(self):
"""
data = super().get_meta_description()
data.update({
'source': str(self.entry_point),
'source': self.entry_point.name + ' = ' + self.entry_point.value,
})
return data
14 changes: 9 additions & 5 deletions test/plugins/test_plugins_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import os
import sys

import pkg_resources
import pytest

try:
import importlib.metadata as importlib_metadata
except ImportError:
# TODO: remove fallback when dropping py3.9
import importlib_metadata

from sopel.plugins import handlers


Expand Down Expand Up @@ -62,15 +67,14 @@ def test_get_label_pyfile_loaded(plugin_tmpfile):


def test_get_label_entrypoint(plugin_tmpfile):
# generate setuptools Distribution object
# set up for manual load/import
distrib_dir = os.path.dirname(plugin_tmpfile.strpath)
distrib = pkg_resources.Distribution(distrib_dir)
sys.path.append(distrib_dir)

# load the entry point
try:
entry_point = pkg_resources.EntryPoint(
'test_plugin', 'file_mod', dist=distrib)
entry_point = importlib_metadata.EntryPoint(
'test_plugin', 'file_mod', 'sopel.plugins')
plugin = handlers.EntryPointPlugin(entry_point)
plugin.load()
finally:
Expand Down
14 changes: 9 additions & 5 deletions test/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

import sys

import pkg_resources
import pytest

try:
import importlib.metadata as importlib_metadata
except ImportError:
# TODO: remove fallback when dropping py3.9
import importlib_metadata

from sopel import plugins


Expand Down Expand Up @@ -132,14 +137,13 @@ def test_plugin_load_entry_point(tmpdir):
mod_file = root.join('file_mod.py')
mod_file.write(MOCK_MODULE_CONTENT)

# generate setuptools Distribution object
distrib = pkg_resources.Distribution(root.strpath)
# set up for manual load/import
sys.path.append(root.strpath)

# load the entry point
try:
entry_point = pkg_resources.EntryPoint(
'test_plugin', 'file_mod', dist=distrib)
entry_point = importlib_metadata.EntryPoint(
'test_plugin', 'file_mod', 'sopel.plugins')
plugin = plugins.handlers.EntryPointPlugin(entry_point)
plugin.load()
finally:
Expand Down