Skip to content

Commit

Permalink
Merge pull request #372 from neutrinoceros/prep_rel_2.9.5
Browse files Browse the repository at this point in the history
REL: prepare release 2.9.5
  • Loading branch information
ngoldbaum authored Feb 23, 2023
2 parents cdf3d78 + 2b8af77 commit d625b71
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
16 changes: 15 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@
History
=======

2.9.5 (2023-02-22)
------------------

* Fix a regression where arrays elements with dtype ``'int8'`` would not compare to floats
as intended. See `PR #371 <https://github.com/yt-project/unyt/pull/371>`_. Thank you to
Clément Robert (@neutrinoceros on GitHub) and Nathan Goldbaum (@ngoldbaum on GitHub) for
the contribution.

* Raise an error in case an array element is assigned to a new value with incompatible
units. See `PR #375 <https://github.com/yt-project/unyt/pull/375>`_ and `PR #376
<https://github.com/yt-project/unyt/pull/376>`_. Thank you to Nathan Goldbaum
(@ngoldbaum on GitHub) for the contribution.


2.9.4 (2023-02-06)
------------------

* Make ``unyt_quantity.from_string`` parse ints.
See `PR #278 <https://github.com/yt-project/unyt/pull/278>`_.
Thank you to Nahan Goldbaum (@ngoldbaum on GitHub) for the contribution.
Thank you to Nathan Goldbaum (@ngoldbaum on GitHub) for the contribution.
* TST: migrate from tox-pyenv to tox-gh-actions #344
See `PR #344 <https://github.com/yt-project/unyt/pull/344>`_.
Thank you to Clément Robert (@neutrinoceros on GitHub) for the contribution.
Expand Down
15 changes: 14 additions & 1 deletion unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@

NULL_UNIT = Unit()
POWER_MAPPING = {multiply: lambda x: x, divide: lambda x: 2 - x}
DISALLOWED_DTYPES = ("S", "U", "a", "O", "M", "m", "b")
DISALLOWED_DTYPES = (
"S", # bytestring
"a", # bytestring
"U", # (unicode) bytes
"O", # Python object
"M", # datetime
"m", # timedelta
)

__doctest_requires__ = {
("unyt_array.from_pint", "unyt_array.to_pint"): ["pint"],
Expand Down Expand Up @@ -1721,6 +1728,12 @@ def __getitem__(self, item):
pass
return ret

def __setitem__(self, item, value):
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, /):
"""
Power function, over-rides the ufunc as
Expand Down
25 changes: 25 additions & 0 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2739,3 +2739,28 @@ def test_string_comparison():
a = 1 * cm
assert not (a == "hello")
assert a != "hello"


def test_int8_comparison():
# see regression https://github.com/yt-project/unyt/issues/369
a = unyt_array(np.zeros(5, dtype=np.int8))
assert all(e == 0 for e in a)


def test_setitem():
# see https://github.com/yt-project/unyt/issues/373
a = [1, 2, 3] * cm
a[1] = 2 * m
assert a[1].value == 200
assert a[1].units == cm

with pytest.raises(UnitConversionError):
a[1] = 2 * g

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 d625b71

Please sign in to comment.