nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: yt-project/unyt
Pull request #376 (2023-02-23 17:14:27)

check if doing setitem with a dimensionless quantity
Apparently yt does this in a few places and I think it makes sense to allow it.


Patch:
diff --git a/unyt/array.py b/unyt/array.py
index c4aab72..b9bdb35 100644
--- a/unyt/array.py
+++ b/unyt/array.py
@@ -1726,8 +1726,9 @@ class unyt_array(np.ndarray):
         return ret
 
     def __setitem__(self, item, value):
-        if hasattr(value, "units") and value.units != self.units:
-            value = value.to(self.units)
+        if hasattr(value, "units"):
+            if value.units != self.units and value.units != NULL_UNIT:
+                value = value.to(self.units)
         super().__setitem__(item, value)
 
     def __pow__(self, p, mod=None, /):


Test patch:
diff --git a/unyt/tests/test_unyt_array.py b/unyt/tests/test_unyt_array.py
index 66faa7e..3f9ef73 100644
--- a/unyt/tests/test_unyt_array.py
+++ b/unyt/tests/test_unyt_array.py
@@ -2767,3 +2767,7 @@ def test_setitem():
     a[1] = 2
     assert a[1].value == 2
     assert a[1].units == cm
+
+    a[1] = unyt_quantity(2)
+    assert a[1].value == 2
+    assert a[1].units == cm
Repository: yt-project/unyt
Pull request #463 (2023-11-01 21:51:06)

BUG: fix an issue where array functions would raise UnitConsistencyError on unyt arrays using non-default unit registries
Address the first part of #462



Patch:
diff --git a/unyt/_array_functions.py b/unyt/_array_functions.py
index ded81b4..0585df2 100644
--- a/unyt/_array_functions.py
+++ b/unyt/_array_functions.py
@@ -216,8 +216,8 @@ def _validate_units_consistency(objs):
     # because it's already a necessary condition for numpy to use our
     # custom implementations
     units = get_units(objs)
-    sunits = set(units)
-    if len(sunits) == 1:
+    unique_units = set(units)
+    if len(unique_units) == 1 or all(u.is_dimensionless for u in unique_units):
         return units[0]
     else:
         raise UnitInconsistencyError(*units)


Test patch:
diff --git a/unyt/tests/test_array_functions.py b/unyt/tests/test_array_functions.py
index 3d9fa7f..8e59f50 100644
--- a/unyt/tests/test_array_functions.py
+++ b/unyt/tests/test_array_functions.py
@@ -18,6 +18,8 @@ from unyt.exceptions import (
     UnytError,
 )
 from unyt.testing import assert_array_equal_units
+from unyt.unit_object import Unit
+from unyt.unit_registry import UnitRegistry
 
 NUMPY_VERSION = Version(version("numpy"))
 
@@ -265,6 +267,23 @@ def test_wrapping_completeness():
         assert function in all_funcs
 
 
+@pytest.mark.parametrize(
+    "arrays",
+    [
+        [np.array([1]), [2] * Unit()],
+        [np.array([1]), [2] * Unit(registry=UnitRegistry())],
+        # [[1], [2] * Unit()],
+    ],
+)
+def test_unit_validation(arrays):
+    # see https://github.com/yt-project/unyt/issues/462
+    # numpy.concatenate isn't essential to this test
+    # what we're really testing is the unit consistency validation
+    # underneath, but we do so using public API
+    res = np.concatenate(arrays)
+    assert res.units.is_dimensionless
+
+
 def test_array_repr():
     arr = [1, 2, 3] * cm
     assert re.fullmatch(r"unyt_array\(\[1, 2, 3\], (units=)?'cm'\)", np.array_repr(arr))
Repository: yt-project/unyt
Pull request #478 (2023-11-29 14:57:50)

BUG: fix an issue where array functions would raise UnitInconsistencyError operands' units differ by some dimensionless factor
Adding the test first, will push the fix after CI goes red.

neutrinoceros: @jzuhone if you agree to this fix, I think that it could justify a 3.0.2 release

Patch:
diff --git a/unyt/_array_functions.py b/unyt/_array_functions.py
index 01e163b..102018a 100644
--- a/unyt/_array_functions.py
+++ b/unyt/_array_functions.py
@@ -220,8 +220,12 @@ def _validate_units_consistency(objs):
     # because it's already a necessary condition for numpy to use our
     # custom implementations
     units = get_units(objs)
-    unique_units = set(units)
-    if len(unique_units) == 1 or all(u.is_dimensionless for u in unique_units):
+
+    # filtering unique units using equality comparison implicitly allows for units
+    # which have a dimensionless**(n) component to be treated as one and the same
+    unique_units = [units[0], *(u for u in units if u != units[0])]
+
+    if len(unique_units) == 1:
         return units[0]
     else:
         raise UnitInconsistencyError(*units)


Test patch:
diff --git a/unyt/tests/test_array_functions.py b/unyt/tests/test_array_functions.py
index bbb37d2..84a343b 100644
--- a/unyt/tests/test_array_functions.py
+++ b/unyt/tests/test_array_functions.py
@@ -284,6 +284,15 @@ def test_unit_validation(arrays):
     assert res.units.is_dimensionless
 
 
+def test_unit_validation_dimensionless_factors():
+    # see https://github.com/yt-project/unyt/issues/477
+    # numpy.concatenate isn't essential to this test
+    # what we're really testing is the unit consistency validation
+    # underneath, but we do so using public API
+    res = np.concatenate([[1] * cm, unyt_array([1], "cm*dimensionless")])
+    assert res.units is cm
+
+
 def test_array_repr():
     arr = [1, 2, 3] * cm
     assert re.fullmatch(r"unyt_array\(\[1, 2, 3\], (units=)?'cm'\)", np.array_repr(arr))