1
1
import contextlib
2
+ import importlib .metadata
2
3
import inspect
3
4
import unittest
4
5
import warnings
6
+ from importlib .metadata import PackageNotFoundError
5
7
from typing import Callable
6
8
from unittest import mock
7
9
@@ -31,8 +33,10 @@ def with_requires(*requirements):
31
33
This test case runs only when `numpy>=1.18` is installed.
32
34
33
35
>>> from cupy import testing
36
+ ...
37
+ ...
34
38
... class Test(unittest.TestCase):
35
- ... @testing.with_requires(' numpy>=1.18' )
39
+ ... @testing.with_requires(" numpy>=1.18" )
36
40
... def test_for_numpy_1_18(self):
37
41
... pass
38
42
@@ -41,8 +45,8 @@ def with_requires(*requirements):
41
45
run a given test case.
42
46
43
47
"""
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 )
46
50
47
51
48
52
def installed (* specifiers ):
@@ -52,14 +56,18 @@ def installed(*specifiers):
52
56
Args:
53
57
specifiers: Version specifiers (e.g., `numpy>=1.20.0`).
54
58
"""
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
58
61
59
62
for spec in specifiers :
63
+ req = Requirement (spec )
60
64
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 )):
63
71
return False
64
72
return True
65
73
0 commit comments