Skip to content

Commit

Permalink
Use importlib instead of pkg_resources (#643)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ndevenish authored Jun 29, 2023
1 parent 6c6d665 commit 71d4ec0
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
11 changes: 3 additions & 8 deletions libtbx_refresh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import contextlib
import importlib
import inspect
import io
import os
Expand All @@ -17,11 +18,6 @@
except ModuleNotFoundError:
pass

try:
import pkg_resources
except ModuleNotFoundError:
pkg_resources = None


def _install_setup_readonly_fallback(package_name: str):
"""
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions newsfragments/643.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move from deprecated pkg_resources to importlib.
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/dxtbx/format/Registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(","))
Expand Down
6 changes: 2 additions & 4 deletions src/dxtbx/model/experiment_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections
import copy
import errno
import importlib.metadata
import itertools
import json
import logging
Expand All @@ -11,8 +12,6 @@
import pickle
from typing import Any, Callable, Generator, Iterable

import pkg_resources

import dxtbx.datablock
from dxtbx.datablock import (
BeamComparison,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions src/dxtbx/model/profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

import importlib.metadata
import logging

import pkg_resources


class ProfileModelFactory:
"""
Expand All @@ -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(
Expand Down

0 comments on commit 71d4ec0

Please sign in to comment.