From 71d4ec04c95739c061d12b551d9cb9f86811134f Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Thu, 29 Jun 2023 22:24:38 +0100 Subject: [PATCH] Use importlib instead of pkg_resources (#643) pkg_resources is now deprecated, as importlib has covered most use-cases since python 3.8. This removes the code causing warnings in downstream tests. Also suppress numpy 'product' warnings when running pytest. --- libtbx_refresh.py | 11 +++-------- newsfragments/643.misc | 1 + pytest.ini | 1 + src/dxtbx/format/Registry.py | 5 ++--- src/dxtbx/model/experiment_list.py | 6 ++---- src/dxtbx/model/profile.py | 5 ++--- 6 files changed, 11 insertions(+), 18 deletions(-) create mode 100644 newsfragments/643.misc diff --git a/libtbx_refresh.py b/libtbx_refresh.py index f80786519..eef867e0f 100644 --- a/libtbx_refresh.py +++ b/libtbx_refresh.py @@ -1,6 +1,7 @@ from __future__ import annotations import contextlib +import importlib import inspect import io import os @@ -17,11 +18,6 @@ except ModuleNotFoundError: pass -try: - import pkg_resources -except ModuleNotFoundError: - pkg_resources = None - def _install_setup_readonly_fallback(package_name: str): """ @@ -67,9 +63,8 @@ def _install_setup_readonly_fallback(package_name: str): if import_path not in sys.path: sys.path.insert(0, import_path) - # ...and add to the existing pkg_resources working_set - if pkg_resources: - pkg_resources.working_set.add_entry(import_path) + # ...and make sure it is picked up by the import system + importlib.invalidate_caches() # This is already generated by this point, but will get picked up # on the second libtbx.refresh. diff --git a/newsfragments/643.misc b/newsfragments/643.misc new file mode 100644 index 000000000..0780b3d13 --- /dev/null +++ b/newsfragments/643.misc @@ -0,0 +1 @@ +Move from deprecated pkg_resources to importlib. diff --git a/pytest.ini b/pytest.ini index a3adf716c..a89237729 100644 --- a/pytest.ini +++ b/pytest.ini @@ -5,6 +5,7 @@ filterwarnings = ignore:numpy.dtype size changed:RuntimeWarning ignore:Datablocks are deprecated:UserWarning ignore:Deprecated call to `pkg_resources.declare_namespace:DeprecationWarning + ignore:`product` is deprecated as of NumPy:DeprecationWarning:h5py|numpy junit_family = legacy markers = regression: dxtbx regression test diff --git a/src/dxtbx/format/Registry.py b/src/dxtbx/format/Registry.py index b7034a22e..03dc39e0d 100644 --- a/src/dxtbx/format/Registry.py +++ b/src/dxtbx/format/Registry.py @@ -5,12 +5,11 @@ """ from __future__ import annotations +import importlib.metadata import os import typing from typing import Callable -import pkg_resources - from dxtbx.util import get_url_scheme if typing.TYPE_CHECKING: @@ -35,7 +34,7 @@ def get_format_class_index() -> dict[str, tuple[Callable[[], type[Format]], list """ if not hasattr(get_format_class_index, "cache"): class_index = {} - for e in pkg_resources.iter_entry_points("dxtbx.format"): + for e in importlib.metadata.entry_points()["dxtbx.format"]: if ":" in e.name: format_name, base_classes_str = e.name.split(":", 1) base_classes = tuple(base_classes_str.split(",")) diff --git a/src/dxtbx/model/experiment_list.py b/src/dxtbx/model/experiment_list.py index 9770129ed..d415e1ccd 100644 --- a/src/dxtbx/model/experiment_list.py +++ b/src/dxtbx/model/experiment_list.py @@ -3,6 +3,7 @@ import collections import copy import errno +import importlib.metadata import itertools import json import logging @@ -11,8 +12,6 @@ import pickle from typing import Any, Callable, Generator, Iterable -import pkg_resources - import dxtbx.datablock from dxtbx.datablock import ( BeamComparison, @@ -323,7 +322,6 @@ def decode(self): # a sensible experiment. el = ExperimentList() for eobj in self._obj["experiment"]: - # Get the models identifier = eobj.get("identifier", "") beam = self._lookup_model("beam", eobj) @@ -467,7 +465,7 @@ def _lookup_model(self, name, experiment_dict): @staticmethod def _scaling_model_from_dict(obj): """Get the scaling model from a dictionary.""" - for entry_point in pkg_resources.iter_entry_points("dxtbx.scaling_model_ext"): + for entry_point in importlib.metadata.entry_points()["dxtbx.scaling_model_ext"]: if entry_point.name == obj["__id__"]: return entry_point.load().from_dict(obj) diff --git a/src/dxtbx/model/profile.py b/src/dxtbx/model/profile.py index 0fbb0434b..ba3852586 100644 --- a/src/dxtbx/model/profile.py +++ b/src/dxtbx/model/profile.py @@ -1,9 +1,8 @@ from __future__ import annotations +import importlib.metadata import logging -import pkg_resources - class ProfileModelFactory: """ @@ -17,7 +16,7 @@ def from_dict(obj): """ if obj is None: return None - for entry_point in pkg_resources.iter_entry_points("dxtbx.profile_model"): + for entry_point in importlib.metadata.entry_points()["dxtbx.profile_model"]: if entry_point.name == obj["__id__"]: return entry_point.load().from_dict(obj) logging.getLogger("dxtbx.model.profile").warn(