11import contextlib
2+ import importlib .metadata
23import inspect
34import unittest
45import warnings
6+ from importlib .metadata import PackageNotFoundError
57from typing import Callable
68from 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
4852def 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