Skip to content
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

ENH: drop dependency to packaging, use tuples to parse and compare version numbers #3604

Closed
Closed
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
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ install_requires =
matplotlib!=3.4.2,>=2.0.2,<3.6
more-itertools>=8.4
numpy>=1.13.3
packaging>=20.9
pyyaml>=4.2b1
setuptools>=19.6
sympy>=1.2,<1.9
Expand Down
20 changes: 10 additions & 10 deletions yt/frontends/open_pmd/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from re import match

import numpy as np
from packaging.version import Version

from yt.data_objects.index_subobjects.grid_patch import AMRGridPatch
from yt.data_objects.static_output import Dataset
Expand All @@ -13,11 +12,12 @@
from yt.frontends.open_pmd.misc import get_component, is_const_component
from yt.funcs import setdefaultattr
from yt.geometry.grid_geometry_handler import GridIndex
from yt.utilities._version import version_tuple
from yt.utilities.file_handler import HDF5FileHandler, warn_h5py
from yt.utilities.logger import ytLogger as mylog
from yt.utilities.on_demand_imports import _h5py as h5py

ompd_known_versions = [Version(_) for _ in ("1.0.0", "1.0.1", "1.1.0")]
ompd_known_versions = [(1, 0, 0), (1, 0, 1), (1, 1, 0)]
opmd_required_attributes = ["openPMD", "basePath"]


Expand Down Expand Up @@ -443,7 +443,7 @@ def __init__(
):
self._handle = HDF5FileHandler(filename)
self.gridsize = kwargs.pop("open_pmd_virtual_gridsize", 10 ** 9)
self.standard_version = Version(self._handle.attrs["openPMD"].decode())
self.standard_version = version_tuple(self._handle.attrs["openPMD"].decode())
self.iteration = kwargs.pop("iteration", None)
self._set_paths(self._handle, path.dirname(filename), self.iteration)
Dataset.__init__(
Expand Down Expand Up @@ -508,7 +508,7 @@ def _set_paths(self, handle, path, iteration):
self.meshes_path = self._handle["/"].attrs["meshesPath"].decode()
handle[self.base_path + self.meshes_path]
except (KeyError):
if self.standard_version <= Version("1.1.0"):
if self.standard_version <= (1, 1, 0):
mylog.info(
"meshesPath not present in file. "
"Assuming file contains no meshes and has a domain extent of 1m^3!"
Expand All @@ -520,7 +520,7 @@ def _set_paths(self, handle, path, iteration):
self.particles_path = self._handle["/"].attrs["particlesPath"].decode()
handle[self.base_path + self.particles_path]
except (KeyError):
if self.standard_version <= Version("1.1.0"):
if self.standard_version <= (1, 1, 0):
mylog.info(
"particlesPath not present in file."
" Assuming file contains no particles!"
Expand Down Expand Up @@ -589,7 +589,7 @@ def _parse_parameter_file(self):
self.domain_left_edge = np.append(dle, np.zeros(3 - len(dle)))
self.domain_right_edge = np.append(dre, np.ones(3 - len(dre)))
except (KeyError, TypeError, AttributeError):
if self.standard_version <= Version("1.1.0"):
if self.standard_version <= (1, 1, 0):
self.dimensionality = 3
self.domain_dimensions = np.ones(3, dtype=np.float64)
self.domain_left_edge = np.zeros(3, dtype=np.float64)
Expand All @@ -609,8 +609,8 @@ def _is_valid(cls, filename, *args, **kwargs):
for i in opmd_required_attributes:
if i not in attrs:
return False

if Version(f.attrs["openPMD"].decode()) not in ompd_known_versions:
version = version_tuple(f.attrs["openPMD"].decode())
if version not in ompd_known_versions:
return False

if f.attrs["iterationEncoding"].decode() == "fileBased":
Expand Down Expand Up @@ -672,8 +672,8 @@ def _is_valid(cls, filename, *args, **kwargs):
for i in opmd_required_attributes:
if i not in attrs:
return False

if Version(f.attrs["openPMD"].decode()) not in ompd_known_versions:
version = version_tuple(f.attrs["openPMD"].decode())
if version not in ompd_known_versions:
return False

if f.attrs["iterationEncoding"].decode() == "groupBased":
Expand Down
4 changes: 2 additions & 2 deletions yt/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import matplotlib
import numpy as np
from more_itertools import always_iterable, collapse, first
from packaging.version import Version
from tqdm import tqdm

from yt.units import YTArray, YTQuantity
from yt.utilities._version import MPL_VERSION
from yt.utilities.exceptions import YTInvalidWidthError
from yt.utilities.logger import ytLogger as mylog
from yt.utilities.on_demand_imports import _requests as requests
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def matplotlib_style_context(style_name=None, after_reset=False):
import matplotlib

style_name = {"mathtext.fontset": "cm"}
if Version(matplotlib.__version__) >= Version("3.3.0"):
if MPL_VERSION >= (3, 3, 0):
style_name["mathtext.fallback"] = "cm"
else:
style_name["mathtext.fallback_to_cm"] = True
Expand Down
23 changes: 23 additions & 0 deletions yt/utilities/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
from typing import Tuple

import matplotlib


def version_tuple(version: str) -> Tuple[int, ...]:
elems = version.split(".")
if len(elems) > 3:
elems = elems[:3]

if not elems[-1].isnumeric():
# allow alpha/beta/release candidate versions
match = re.search(r"^\d+", elems[-1])
if match is None:
elems.pop()
else:
elems[-1] = match.group()

return tuple(int(_) for _ in elems)


MPL_VERSION = version_tuple(matplotlib.__version__)
16 changes: 0 additions & 16 deletions yt/utilities/on_demand_imports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys

from packaging.version import Version


class NotAModule:
"""
Expand Down Expand Up @@ -357,20 +355,6 @@ class h5py_imports:
_name = "h5py"
_err = None

def __init__(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropping this completely because setup.cfg requires h5py >= 3.1 anyway

try:
import h5py

if Version(h5py.__version__) < Version("2.4.0"):
self._err = RuntimeError(
"yt requires h5py version 2.4.0 or newer, "
"please update h5py with e.g. `python -m pip install -U h5py` "
"and try again"
)
except ImportError:
pass
super().__init__()

_File = None

@property
Expand Down
8 changes: 2 additions & 6 deletions yt/visualization/_commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import sys
from typing import Optional, Type

import matplotlib
from packaging.version import Version

from yt.utilities._version import MPL_VERSION
from yt.utilities.logger import ytLogger as mylog

from ._mpl_imports import (
Expand All @@ -15,14 +13,12 @@
FigureCanvasSVG,
)

MPL_VERSION = Version(matplotlib.__version__)

DEFAULT_FONT_PROPERTIES = {
"family": "stixgeneral",
"size": 18,
}

if MPL_VERSION >= Version("3.4"):
if MPL_VERSION >= (3, 4, 0):
DEFAULT_FONT_PROPERTIES["math_fontfamily"] = "cm"

SUPPORTED_FORMATS = frozenset(FigureCanvasBase.get_supported_filetypes().keys())
Expand Down
11 changes: 4 additions & 7 deletions yt/visualization/base_plot_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import matplotlib
import numpy as np
from packaging.version import Version

from yt.funcs import (
get_brewer_cmap,
Expand All @@ -11,6 +10,7 @@
matplotlib_style_context,
mylog,
)
from yt.utilities._version import MPL_VERSION

from ._commons import get_canvas, validate_image_name

Expand Down Expand Up @@ -132,10 +132,8 @@ def save(self, name, mpl_kwargs=None, canvas=None):

if mpl_kwargs is None:
mpl_kwargs = {}
if "papertype" not in mpl_kwargs and Version(matplotlib.__version__) < Version(
"3.3.0"
):
mpl_kwargs["papertype"] = "auto"
if MPL_VERSION < (3, 3, 0):
mpl_kwargs.setdefault("papertype", "auto")

name = validate_image_name(name)

Expand Down Expand Up @@ -216,8 +214,7 @@ def _init_image(self, data, cbnorm, cblinthresh, cmap, extent, aspect):
cblinthresh = np.nanmin(np.absolute(data)[data != 0])

cbnorm_kwargs.update(dict(linthresh=cblinthresh))
MPL_VERSION = Version(matplotlib.__version__)
if MPL_VERSION >= Version("3.2.0"):
if MPL_VERSION >= (3, 2, 0):
# note that this creates an inconsistency between mpl versions
# since the default value previous to mpl 3.4.0 is np.e
# but it is only exposed since 3.2.0
Expand Down
10 changes: 4 additions & 6 deletions yt/visualization/color_maps.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import numpy as np
from matplotlib import __version__ as mpl_ver, cm as mcm, colors as cc
from packaging.version import Version
from matplotlib import cm as mcm, colors as cc

from . import _colormap_data as _cm
from yt.utilities._version import MPL_VERSION

MPL_VERSION = Version(mpl_ver)
del mpl_ver
from . import _colormap_data as _cm


def is_colormap(cmap):
Expand Down Expand Up @@ -260,7 +258,7 @@ def show_colormaps(subset="all", filename=None):
"to be 'all', 'yt_native', or a list of "
"valid colormap names."
) from e
if Version("2.0.0") <= MPL_VERSION < Version("2.2.0"):
if (2, 0, 0) <= MPL_VERSION < (2, 2, 0):
# the reason we do this filtering is to avoid spurious warnings in CI when
# testing against old versions of matplotlib (currently not older than 2.0.x)
# and we can't easily filter warnings at the level of the relevant test itself
Expand Down
10 changes: 4 additions & 6 deletions yt/visualization/plot_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import numpy as np
from more_itertools import always_iterable
from mpl_toolkits.axes_grid1 import ImageGrid
from packaging.version import Version
from unyt.exceptions import UnitConversionError

from yt._maintenance.deprecation import issue_deprecation_warning
Expand All @@ -18,6 +17,7 @@
from yt.units.unit_object import Unit
from yt.units.unit_registry import UnitParseError
from yt.units.yt_array import YTArray, YTQuantity
from yt.utilities._version import MPL_VERSION
from yt.utilities.exceptions import (
YTCannotParseUnitDisplayName,
YTDataTypeUnsupported,
Expand Down Expand Up @@ -64,8 +64,6 @@ def zip_equal(*args):
return zip(*args, strict=True)


MPL_VERSION = Version(matplotlib.__version__)

# Some magic for dealing with pyparsing being included or not
# included in matplotlib (not in gentoo, yes in everything else)
try:
Expand Down Expand Up @@ -1223,7 +1221,7 @@ def _setup_plots(self):
self.plots[f].cax.minorticks_on()

elif self._field_transform[f] == symlog_transform:
if Version("3.2.0") <= MPL_VERSION < Version("3.5.0"):
if (3, 2, 0) <= MPL_VERSION < (3, 5, 0):
# no known working method to draw symlog minor ticks
# see https://github.com/yt-project/yt/issues/3535
pass
Expand All @@ -1232,13 +1230,13 @@ def _setup_plots(self):
np.log10(self.plots[f].cb.norm.linthresh)
)
mticks = get_symlog_minorticks(flinthresh, vmin, vmax)
if MPL_VERSION < Version("3.5.0"):
if MPL_VERSION < (3, 5, 0):
# https://github.com/matplotlib/matplotlib/issues/21258
mticks = self.plots[f].image.norm(mticks)
self.plots[f].cax.yaxis.set_ticks(mticks, minor=True)

elif self._field_transform[f] == log_transform:
if MPL_VERSION >= Version("3.0.0"):
if MPL_VERSION >= (3, 0, 0):
self.plots[f].cax.minorticks_on()
self.plots[f].cax.xaxis.set_visible(False)
else:
Expand Down
6 changes: 2 additions & 4 deletions yt/visualization/profile_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import numpy as np
from matplotlib.font_manager import FontProperties
from more_itertools.more import always_iterable, unzip
from packaging.version import Version

from yt.data_objects.profiles import create_profile, sanitize_field_tuple_keys
from yt.data_objects.static_output import Dataset
from yt.frontends.ytdata.data_structures import YTProfileDataset
from yt.funcs import is_sequence, iter_fields, matplotlib_style_context
from yt.utilities._version import MPL_VERSION
from yt.utilities.exceptions import YTNotInsideNotebook
from yt.utilities.logger import ytLogger as mylog

Expand All @@ -30,8 +30,6 @@
validate_plot,
)

MPL_VERSION = Version(matplotlib.__version__)


def invalidate_profile(f):
@wraps(f)
Expand Down Expand Up @@ -1178,7 +1176,7 @@ def _setup_plots(self):
if self._cbar_minorticks[f]:
if self._field_transform[f] == linear_transform:
self.plots[f].cax.minorticks_on()
elif MPL_VERSION < Version("3.0.0"):
elif MPL_VERSION < (3, 0, 0):
# before matplotlib 3 log-scaled colorbars internally used
# a linear scale going from zero to one and did not draw
# minor ticks. Since we want minor ticks, calculate
Expand Down