Skip to content
Open
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
8 changes: 7 additions & 1 deletion docs/extensions/show_all_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ exclude = [
ignore = [
"E501",
"B904",
"B905", # zip-without-explicit-strict
]
select = [
"E",
Expand All @@ -141,6 +140,7 @@ select = [
"I", # isort
"UP", # pyupgrade
"NPY", # numpy specific rules
"RUF007", # zip-instead-of-pairwise
]

[tool.ruff.lint.isort]
Expand Down
3 changes: 2 additions & 1 deletion unyt/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""

import itertools
import token

from sympy import Basic, Float, Integer, Rational, Symbol, sqrt
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions unyt/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is the one place where I used strict=False because it makes sense in this context: when all arguments are passed as keywords, args is empty an d names_of_args is effectively the same as list(kwargs.keys())

):
if arg_name in arg_units: # function argument needs to be checked
dimension = arg_units[arg_name]
if not _has_dimensions(arg_value, dimension):
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions unyt/tests/sample_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
),
)

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions unyt/tests/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down