Skip to content

Fix DeprecationWarning: pkg_resources #2156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading