Skip to content

Commit

Permalink
Major changes for numpy compatibility and overhaul the stubs file (#33)
Browse files Browse the repository at this point in the history
- implement `ValueArray.__array_ufunc__` for performance
- overhaul the stubs file
  • Loading branch information
NoureldinYosri authored Jan 31, 2025
2 parents 9781922 + 51d087a commit af9ba68
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 238 deletions.
8 changes: 3 additions & 5 deletions test/cython/test_with_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def deep_equal(a: WithUnit, b: WithUnit) -> bool:
bool, whether a and b have exactly the same properties.
"""
if isinstance(a, ValueArray):
if not np.array_equal(a, b):
if not np.array_equal(a, b): # type: ignore[arg-type]
return False
else:
if a != b:
Expand Down Expand Up @@ -473,10 +473,8 @@ def test_non_zero() -> None:


def test_numpy_method_is_finite() -> None:
with pytest.raises(TypeError):
np.isfinite(val([], units=m))
with pytest.raises(TypeError):
np.isfinite(val([1], units=m))
assert np.all(np.isfinite(val([], units=m)))
assert np.all(np.isfinite(val([1], units=m)))

v = val([2, 3, -2, float('nan'), float('inf')], conv(1, 2, 3, 4))
assert np.array_equal(np.isfinite(v), [True, True, True, False, False])
Expand Down
6 changes: 3 additions & 3 deletions test/test_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ def test_hash() -> None:
def test_numpy_sqrt() -> None:
from tunits.units import m, km, cm

u: Value = np.sqrt(8 * km * m) - cm # type: ignore[assignment]
u: Value = np.sqrt(8 * km * m) - cm
v = 8943.27191 * cm
assert np.isclose(u / v, 1)

u = np.sqrt(8 * km / m) # type: ignore[assignment]
u = np.sqrt(8 * km / m)
assert np.isclose(u, 89.4427191)

u = np.sqrt((8 * km / m).in_base_units()) # type: ignore[assignment]
u = np.sqrt((8 * km / m).in_base_units())
assert np.isclose(u, 89.4427191)


Expand Down
Loading

0 comments on commit af9ba68

Please sign in to comment.