From 9cd14d0a04a3ac985617f80e04dcd6657fc48a35 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 13 Jul 2023 16:13:22 +0200 Subject: [PATCH] replace pkg_resources.parse_version by packaging.version.Version --- src/silx/gui/hdf5/test/test_hdf5.py | 4 ++-- src/silx/gui/plot/backends/BackendMatplotlib.py | 16 ++++++++-------- src/silx/gui/utils/matplotlib.py | 1 - src/silx/math/fft/basefft.py | 6 +++--- src/silx/math/fft/npfft.py | 4 ++-- src/silx/math/fft/test/test_fft.py | 4 ++-- src/silx/utils/test/test_number.py | 6 +++--- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/silx/gui/hdf5/test/test_hdf5.py b/src/silx/gui/hdf5/test/test_hdf5.py index 75a927abc9..40ffd75e60 100755 --- a/src/silx/gui/hdf5/test/test_hdf5.py +++ b/src/silx/gui/hdf5/test/test_hdf5.py @@ -32,7 +32,7 @@ import os import tempfile import numpy -from pkg_resources import parse_version +from packaging.version import Version from contextlib import contextmanager from silx.gui import qt from silx.gui.utils.testutils import TestCaseQt @@ -45,7 +45,7 @@ import pytest -h5py2_9 = parse_version(h5py.version.version) >= parse_version('2.9.0') +h5py2_9 = Version(h5py.version.version) >= Version('2.9.0') @pytest.fixture(scope="class") diff --git a/src/silx/gui/plot/backends/BackendMatplotlib.py b/src/silx/gui/plot/backends/BackendMatplotlib.py index eb32dfa4e0..b12aa78f3f 100755 --- a/src/silx/gui/plot/backends/BackendMatplotlib.py +++ b/src/silx/gui/plot/backends/BackendMatplotlib.py @@ -33,7 +33,7 @@ from typing import Tuple, Union import numpy -from pkg_resources import parse_version as _parse_version +from packaging.version import Version _logger = logging.getLogger(__name__) @@ -494,7 +494,7 @@ def __init__(self, plot, parent=None): # when getting the limits at the expense of a replot self._dirtyLimits = True self._axesDisplayed = True - self._matplotlibVersion = _parse_version(matplotlib.__version__) + self._matplotlibVersion = Version(matplotlib.__version__) self.fig = Figure() self.fig.set_facecolor("w") @@ -527,7 +527,7 @@ def __init__(self, plot, parent=None): self.ax2.set_autoscaley_on(True) # this works but the figure color is left - if self._matplotlibVersion < _parse_version('2'): + if self._matplotlibVersion < Version('2'): self.ax.set_axis_bgcolor('none') else: self.ax.set_facecolor('none') @@ -1175,7 +1175,7 @@ def setXAxisLogarithmic(self, flag): # Workaround for matplotlib 2.1.0 when one tries to set an axis # to log scale with both limits <= 0 # In this case a draw with positive limits is needed first - if flag and self._matplotlibVersion >= _parse_version('2.1.0'): + if flag and self._matplotlibVersion >= Version('2.1.0'): xlim = self.ax.get_xlim() if xlim[0] <= 0 and xlim[1] <= 0: self.ax.set_xlim(1, 10) @@ -1187,7 +1187,7 @@ def setXAxisLogarithmic(self, flag): def setYAxisLogarithmic(self, flag): # Workaround for matplotlib 2.0 issue with negative bounds # before switching to log scale - if flag and self._matplotlibVersion >= _parse_version('2.0.0'): + if flag and self._matplotlibVersion >= Version('2.0.0'): redraw = False for axis, dataRangeIndex in ((self.ax, 1), (self.ax2, 2)): ylim = axis.get_ylim() @@ -1300,7 +1300,7 @@ def _synchronizeBackgroundColors(self): if self.ax.get_frame_on(): self.fig.patch.set_facecolor(backgroundColor) - if self._matplotlibVersion < _parse_version('2'): + if self._matplotlibVersion < Version('2'): self.ax.set_axis_bgcolor(dataBackgroundColor) else: self.ax.set_facecolor(dataBackgroundColor) @@ -1501,7 +1501,7 @@ def draw(self): # Starting with mpl 2.1.0, toggling autoscale raises a ValueError # in some situations. See #1081, #1136, #1163, - if self._matplotlibVersion >= _parse_version("2.0.0"): + if self._matplotlibVersion >= Version("2.0.0"): try: FigureCanvasQTAgg.draw(self) except ValueError as err: @@ -1557,7 +1557,7 @@ def replot(self): self.draw() # Workaround issue of rendering overlays with some matplotlib versions - if (_parse_version('1.5') <= self._matplotlibVersion < _parse_version('2.1') and + if (Version('1.5') <= self._matplotlibVersion < Version('2.1') and not hasattr(self, '_firstReplot')): self._firstReplot = False if self._hasOverlays(): diff --git a/src/silx/gui/utils/matplotlib.py b/src/silx/gui/utils/matplotlib.py index c1d4fc411a..c24749be70 100644 --- a/src/silx/gui/utils/matplotlib.py +++ b/src/silx/gui/utils/matplotlib.py @@ -36,7 +36,6 @@ import io -from pkg_resources import parse_version import matplotlib import numpy diff --git a/src/silx/math/fft/basefft.py b/src/silx/math/fft/basefft.py index c608fdeb15..e858f14ff3 100644 --- a/src/silx/math/fft/basefft.py +++ b/src/silx/math/fft/basefft.py @@ -23,7 +23,7 @@ # # ###########################################################################*/ import numpy as np -from pkg_resources import parse_version +from packaging.version import Version def check_version(package, required_version): @@ -37,8 +37,8 @@ def check_version(package, required_version): ver = getattr(package, "version") except Exception: return False - req_v = parse_version(required_version) - ver_v = parse_version(ver) + req_v = Version(required_version) + ver_v = Version(ver) return ver_v >= req_v diff --git a/src/silx/math/fft/npfft.py b/src/silx/math/fft/npfft.py index fc7d1c9ab2..3fe0754a03 100644 --- a/src/silx/math/fft/npfft.py +++ b/src/silx/math/fft/npfft.py @@ -24,7 +24,7 @@ # ###########################################################################*/ import numpy as np import warnings -from pkg_resources import parse_version +from packaging.version import Version from .basefft import BaseFFT @@ -76,7 +76,7 @@ def set_fft_norm(self): self.numpy_args_ifft = {"norm": "ortho"} elif self.normalize == "none": # no normalisation on both fft & ifft - if parse_version(np.version.version) < parse_version("1.20"): + if Version(np.version.version) < Version("1.20"): # "backward" & "forward" keywords were introduced in 1.20 and we support numpy >= 1.8 warnings.warn( "Numpy version %s does not allow to non-normalization. Effective normalization will be 'rescale'" diff --git a/src/silx/math/fft/test/test_fft.py b/src/silx/math/fft/test/test_fft.py index 569590a9e7..6f39e0abdb 100644 --- a/src/silx/math/fft/test/test_fft.py +++ b/src/silx/math/fft/test/test_fft.py @@ -28,7 +28,7 @@ import logging import numpy as np import unittest -from pkg_resources import parse_version +from packaging.version import Version import pytest from tempfile import TemporaryDirectory try: @@ -282,7 +282,7 @@ def test_norms_fftw(self): return self._test_norms_with_backend("fftw") @unittest.skipIf( - parse_version(np.version.version) <= parse_version("1.19.5"), + Version(np.version.version) <= Version("1.19.5"), "normalization does not work for numpy <= 1.19.5" ) def test_norms_numpy(self): diff --git a/src/silx/utils/test/test_number.py b/src/silx/utils/test/test_number.py index 61adddd3ef..3af01f8145 100644 --- a/src/silx/utils/test/test_number.py +++ b/src/silx/utils/test/test_number.py @@ -28,7 +28,7 @@ import logging import numpy -import pkg_resources +from packaging.version import Version from silx.utils import number from silx.utils import testutils @@ -115,7 +115,7 @@ def testMantissa80(self): self.skipIfFloat80NotSupported() dtype = number.min_numerical_convertible_type("1000000000.00001013") - if pkg_resources.parse_version(numpy.version.version) <= pkg_resources.parse_version("1.10.4"): + if Version(numpy.version.version) <= Version("1.10.4"): # numpy 1.8.2 -> Debian 8 # Checking a float128 precision with numpy 1.8.2 using abs(diff) is not working. # It looks like the difference is done using float64 (diff == 0.0) @@ -148,7 +148,7 @@ def skipIfFloat80NotSupported(self): def testLosePrecisionUsingFloat80(self): self.skipIfFloat80NotSupported() - if pkg_resources.parse_version(numpy.version.version) <= pkg_resources.parse_version("1.10.4"): + if Version(numpy.version.version) <= Version("1.10.4"): self.skipTest("numpy > 1.10.4 expected") # value does not fit even in a 128 bits mantissa value = "1.0340282366920938463463374607431768211456"