From 06f6e333450e62d8235cb285d00f277fae49d535 Mon Sep 17 00:00:00 2001 From: vlad-perevezentsev Date: Thu, 7 Nov 2024 16:13:50 +0100 Subject: [PATCH] Fix DeprecationWarning: pkg_resources (#2156) * Replace pkg_resources to packaging to avoid DeprecationWarning * A small update cupy with_requires() func * Remove pkg_resources filterwarning --- setup.cfg | 2 -- tests/third_party/cupy/testing/_helper.py | 24 +++++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/setup.cfg b/setup.cfg index 60c8b1f6372..8841f74f290 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/third_party/cupy/testing/_helper.py b/tests/third_party/cupy/testing/_helper.py index fb817ccee9d..38a347e1561 100644 --- a/tests/third_party/cupy/testing/_helper.py +++ b/tests/third_party/cupy/testing/_helper.py @@ -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 @@ -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 @@ -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): @@ -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