Skip to content

Commit 06f6e33

Browse files
Fix DeprecationWarning: pkg_resources (#2156)
* Replace pkg_resources to packaging to avoid DeprecationWarning * A small update cupy with_requires() func * Remove pkg_resources filterwarning
1 parent 9086f45 commit 06f6e33

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

setup.cfg

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ markers =
1313
slow: marks tests as slow (deselect with '-m "not slow"')
1414
multi_gpu: marks tests that require a specified number of GPUs
1515
filterwarnings =
16-
# pkg_resources
17-
ignore:pkg_resources is deprecated as an API:DeprecationWarning
1816
# NumPy arccosh
1917
# Undefined behavior depends on the backend:
2018
# NumPy with OpenBLAS for np.array[1.0] does not raise a warning

tests/third_party/cupy/testing/_helper.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import contextlib
2+
import importlib.metadata
23
import inspect
34
import unittest
45
import warnings
6+
from importlib.metadata import PackageNotFoundError
57
from typing import Callable
68
from unittest import mock
79

@@ -31,8 +33,10 @@ def with_requires(*requirements):
3133
This test case runs only when `numpy>=1.18` is installed.
3234
3335
>>> from cupy import testing
36+
...
37+
...
3438
... class Test(unittest.TestCase):
35-
... @testing.with_requires('numpy>=1.18')
39+
... @testing.with_requires("numpy>=1.18")
3640
... def test_for_numpy_1_18(self):
3741
... pass
3842
@@ -41,8 +45,8 @@ def with_requires(*requirements):
4145
run a given test case.
4246
4347
"""
44-
msg = "requires: {}".format(",".join(requirements))
45-
return _skipif(not installed(requirements), reason=msg)
48+
msg = f"requires: {','.join(requirements)}"
49+
return _skipif(not installed(*requirements), reason=msg)
4650

4751

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

5962
for spec in specifiers:
63+
req = Requirement(spec)
6064
try:
61-
pkg_resources.require(spec)
62-
except pkg_resources.ResolutionError:
65+
found = importlib.metadata.version(req.name)
66+
except PackageNotFoundError:
67+
return False
68+
expected = req.specifier
69+
# If no constraint is given, skip
70+
if expected and (not expected.contains(found, prereleases=True)):
6371
return False
6472
return True
6573

0 commit comments

Comments
 (0)