Skip to content

Commit

Permalink
Fix DeprecationWarning: pkg_resources (#2156)
Browse files Browse the repository at this point in the history
* Replace pkg_resources to packaging to avoid DeprecationWarning

* A small update cupy with_requires() func

* Remove pkg_resources filterwarning
  • Loading branch information
vlad-perevezentsev authored Nov 7, 2024
1 parent 9086f45 commit 06f6e33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ markers =
slow: marks tests as slow (deselect with '-m "not slow"')
multi_gpu: marks tests that require a specified number of GPUs
filterwarnings =
# pkg_resources
ignore:pkg_resources is deprecated as an API:DeprecationWarning
# NumPy arccosh
# Undefined behavior depends on the backend:
# NumPy with OpenBLAS for np.array[1.0] does not raise a warning
Expand Down
24 changes: 16 additions & 8 deletions tests/third_party/cupy/testing/_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import contextlib
import importlib.metadata
import inspect
import unittest
import warnings
from importlib.metadata import PackageNotFoundError
from typing import Callable
from unittest import mock

Expand Down Expand Up @@ -31,8 +33,10 @@ def with_requires(*requirements):
This test case runs only when `numpy>=1.18` is installed.
>>> from cupy import testing
...
...
... class Test(unittest.TestCase):
... @testing.with_requires('numpy>=1.18')
... @testing.with_requires("numpy>=1.18")
... def test_for_numpy_1_18(self):
... pass
Expand All @@ -41,8 +45,8 @@ def with_requires(*requirements):
run a given test case.
"""
msg = "requires: {}".format(",".join(requirements))
return _skipif(not installed(requirements), reason=msg)
msg = f"requires: {','.join(requirements)}"
return _skipif(not installed(*requirements), reason=msg)


def installed(*specifiers):
Expand All @@ -52,14 +56,18 @@ def installed(*specifiers):
Args:
specifiers: Version specifiers (e.g., `numpy>=1.20.0`).
"""
# Delay import of pkg_resources because it is excruciatingly slow.
# See https://github.com/pypa/setuptools/issues/510
import pkg_resources
# Make `packaging` a soft requirement
from packaging.requirements import Requirement

for spec in specifiers:
req = Requirement(spec)
try:
pkg_resources.require(spec)
except pkg_resources.ResolutionError:
found = importlib.metadata.version(req.name)
except PackageNotFoundError:
return False
expected = req.specifier
# If no constraint is given, skip
if expected and (not expected.contains(found, prereleases=True)):
return False
return True

Expand Down

0 comments on commit 06f6e33

Please sign in to comment.