Skip to content

Commit

Permalink
Merge pull request yt-project#376 from ngoldbaum/setitem-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Feb 23, 2023
1 parent 2ab7b7b commit 5e0cf09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,8 +1729,9 @@ def __getitem__(self, item):
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, /):
Expand Down
4 changes: 4 additions & 0 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,3 +2760,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

0 comments on commit 5e0cf09

Please sign in to comment.