diff --git a/docs/extensions/show_all_units.py b/docs/extensions/show_all_units.py index e9507dcc..c9b38a7e 100644 --- a/docs/extensions/show_all_units.py +++ b/docs/extensions/show_all_units.py @@ -149,7 +149,13 @@ def as_rest_table(data, full=False): data = data if data else [["No Data"]] table = [] # max size of each column - sizes = list(map(max, zip(*[[len(str(elt)) for elt in member] for member in data]))) + sizes = [ + max(*args) + for args in zip( + *[[len(str(elt)) for elt in member] for member in data], + strict=True, + ) + ] num_elts = len(sizes) if full: diff --git a/pyproject.toml b/pyproject.toml index 3fd6b8fd..96a63ca8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,7 +129,6 @@ exclude = [ ignore = [ "E501", "B904", - "B905", # zip-without-explicit-strict ] select = [ "E", @@ -141,6 +140,7 @@ select = [ "I", # isort "UP", # pyupgrade "NPY", # numpy specific rules + "RUF007", # zip-instead-of-pairwise ] [tool.ruff.lint.isort] diff --git a/unyt/_parsing.py b/unyt/_parsing.py index f22d2ef9..555b2e46 100644 --- a/unyt/_parsing.py +++ b/unyt/_parsing.py @@ -5,6 +5,7 @@ """ +import itertools import token from sympy import Basic, Float, Integer, Rational, Symbol, sqrt @@ -23,7 +24,7 @@ def _auto_positive_symbol(tokens, local_dict, global_dict): result = [] tokens.append((None, None)) # so zip traverses all tokens - for tok, nextTok in zip(tokens, tokens[1:]): + for tok, nextTok in itertools.pairwise(tokens): tokNum, tokVal = tok nextTokNum, nextTokVal = nextTok if tokNum == token.NAME: diff --git a/unyt/array.py b/unyt/array.py index ac3fe22f..fcf8fc88 100644 --- a/unyt/array.py +++ b/unyt/array.py @@ -1368,7 +1368,7 @@ def from_astropy(cls, arr, unit_registry=None): u = arr _arr = 1.0 * u ap_units = [] - for base, exponent in zip(u.bases, u.powers): + for base, exponent in zip(u.bases, u.powers, strict=True): unit_str = base.to_string() # we have to do this because AstroPy is silly and defines # hour as "h" @@ -2114,7 +2114,7 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): # out_arr is an ndarray out.units = Unit("", registry=self.units.registry) elif isinstance(out, tuple): - for o, oa in zip(out, out_arr): + for o, oa in zip(out, out_arr, strict=True): if o is None: continue o.units = oa.units @@ -2710,7 +2710,7 @@ def loadtxt(fname, dtype="float", delimiter="\t", usecols=None, comments="#"): arrays = [arrays] if usecols is not None: units = [units[col] for col in usecols] - ret = tuple(unyt_array(arr, unit) for arr, unit in zip(arrays, units)) + ret = tuple(unyt_array(arr, unit) for arr, unit in zip(arrays, units, strict=True)) if len(ret) == 1: return ret[0] return ret diff --git a/unyt/dimensions.py b/unyt/dimensions.py index 169413f8..27df9a04 100644 --- a/unyt/dimensions.py +++ b/unyt/dimensions.py @@ -277,7 +277,9 @@ def new_f(*args, **kwargs): If the units do not match. """ - for arg_name, arg_value in chain(zip(names_of_args, args), kwargs.items()): + for arg_name, arg_value in chain( + zip(names_of_args, args, strict=False), kwargs.items() + ): if arg_name in arg_units: # function argument needs to be checked dimension = arg_units[arg_name] if not _has_dimensions(arg_value, dimension): @@ -378,7 +380,7 @@ def new_f(*args, **kwargs): else: result_tuple = (results,) - for result, dimension in zip(result_tuple, r_units): + for result, dimension in zip(result_tuple, r_units, strict=True): if not _has_dimensions(result, dimension): raise TypeError(f"result '{result}' does not match {dimension}") return results diff --git a/unyt/tests/sample_subclasses.py b/unyt/tests/sample_subclasses.py index 3bebd1e4..686b44ea 100644 --- a/unyt/tests/sample_subclasses.py +++ b/unyt/tests/sample_subclasses.py @@ -543,7 +543,7 @@ def _histogramdd(sample, bins=10, range=None, density=None, weights=None, normed bins = D * [bins] helper_results = [ _prepare_array_func_args(s, bins=b, range=r) - for s, b, r in zip(sample, bins, ranges) + for s, b, r in zip(sample, bins, ranges, strict=True) ] if not density: helper_result_w = _prepare_array_func_args(weights=weights) @@ -599,7 +599,7 @@ def _histogramdd(sample, bins=10, range=None, density=None, weights=None, normed counts, tuple( _return_helper(b, helper_result) - for b, helper_result in zip(bins, helper_results) + for b, helper_result in zip(bins, helper_results, strict=False) ), ) @@ -933,7 +933,9 @@ def meshgrid(*xi, **kwargs): # need to iterate over arguments. res = np.meshgrid._implementation(*xi, **kwargs) ret_type = tuple if NUMPY_VERSION >= Version("2.0.0dev0") else list - return ret_type(_copy_extra_attr_if_present(x, r) for (x, r) in zip(xi, res)) + return ret_type( + _copy_extra_attr_if_present(x, r) for (x, r) in zip(xi, res, strict=False) + ) class subclass_uarray(unyt_array): diff --git a/unyt/tests/test_subclass.py b/unyt/tests/test_subclass.py index b5498cf2..2f8cdefa 100644 --- a/unyt/tests/test_subclass.py +++ b/unyt/tests/test_subclass.py @@ -81,8 +81,7 @@ def check_result(x_s, x_u, ignore_values=False): return if type(x_u) in (list, tuple): assert type(x_u) is type(x_s) - assert len(x_u) == len(x_s) - for x_c_i, x_u_i in zip(x_s, x_u): + for x_c_i, x_u_i in zip(x_s, x_u, strict=True): check_result(x_c_i, x_u_i) return # careful, unyt_quantity is a subclass of unyt_array: @@ -788,8 +787,7 @@ def test_histograms(self, func_args, weights, bins_type, density): ) if isinstance(ua_result, tuple): assert isinstance(result, tuple) - assert len(result) == len(ua_result) - for r, ua_r in zip(result, ua_result): + for r, ua_r in zip(result, ua_result, strict=True): check_result(r, ua_r) else: check_result(result, ua_result) diff --git a/unyt/tests/test_unyt_array.py b/unyt/tests/test_unyt_array.py index 11614ab2..3d5dec8a 100644 --- a/unyt/tests/test_unyt_array.py +++ b/unyt/tests/test_unyt_array.py @@ -590,15 +590,15 @@ def test_comparisons(): [True, True, False], ) - for op, answer in zip(ops, answers): + for op, answer in zip(ops, answers, strict=True): operate_and_compare(a1, a2, op, answer) - for op, answer in zip(ops, answers): + for op, answer in zip(ops, answers, strict=True): operate_and_compare(a1, dimless, op, answer) - for op, answer in zip(ops, answers): + for op, answer in zip(ops, answers, strict=True): operate_and_compare(a1, a3, op, answer) - for op, answer in zip(ops, answers): + for op, answer in zip(ops, answers, strict=True): operate_and_compare(a1, a3.in_units("cm"), op, answer) # Check that comparisons with dimensionless quantities work in both @@ -862,7 +862,7 @@ def test_iteration(): """ a = np.arange(3) b = unyt_array(np.arange(3), "cm") - for ia, ib in zip(a, b): + for ia, ib in zip(a, b, strict=True): assert_equal(ia, ib.value) assert_equal(ib.units, b.units) @@ -1153,7 +1153,7 @@ def binary_ufunc_comparison(ufunc, a, b): if isinstance(ret, tuple): assert isinstance(out, tuple) assert len(out) == len(ret) - for o, r in zip(out, ret): + for o, r in zip(out, ret, strict=True): assert_array_equal(r, o) else: assert_array_equal(ret, out)