Skip to content

Commit

Permalink
test: trapezoid should be used for Numpy >= 2 and trapz otherwise
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Feb 15, 2025
1 parent 48106e2 commit d7c69f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pint/testsuite/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,10 @@ def test_cross(self):

# NP2: Remove this when we only support np>=2.0
@helpers.requires_array_function_protocol()
@helpers.requires_numpy_previous_than("2.0")
def test_trapz(self):
helpers.assert_quantity_equal(
np.trapezoid([1.0, 2.0, 3.0, 4.0] * self.ureg.J, dx=1 * self.ureg.m),
np.trapz([1.0, 2.0, 3.0, 4.0] * self.ureg.J, dx=1 * self.ureg.m),
7.5 * self.ureg.J * self.ureg.m,
)

Expand Down
17 changes: 17 additions & 0 deletions pint/testsuite/test_numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,24 @@ def test_numpy_wrap(self):
# TODO (#905 follow-up): test that NotImplemented is returned when upcast types
# present

@helpers.requires_numpy_previous_than("2.0")
def test_trapz(self):
with ExitStack() as stack:
stack.callback(
setattr,
self.ureg,
"autoconvert_offset_to_baseunit",
self.ureg.autoconvert_offset_to_baseunit,
)
self.ureg.autoconvert_offset_to_baseunit = True
t = self.Q_(np.array([0.0, 4.0, 8.0]), "degC")
z = self.Q_(np.array([0.0, 2.0, 4.0]), "m")
helpers.assert_quantity_equal(
np.trapz(t, x=z), self.Q_(1108.6, "kelvin meter")
)

@helpers.requires_numpy_at_least("2.0")
def test_trapezoid(self):
with ExitStack() as stack:
stack.callback(
setattr,
Expand Down

0 comments on commit d7c69f7

Please sign in to comment.