Skip to content

Commit

Permalink
Fix running tests when numpy is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jul 13, 2023
1 parent d83de0b commit 43b25d9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pint/testsuite/benchmarks/test_30_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pint
from pint.compat import np

from ..helpers import requires_numpy

SMALL_VEC_LEN = 3
MID_VEC_LEN = 1_000
Expand All @@ -23,8 +24,10 @@
OP1 = (operator.neg,) # operator.truth,
OP2_CMP = (operator.eq, operator.lt)
OP2_MATH = (operator.add, operator.sub, operator.mul, operator.truediv)
NUMPY_OP2_CMP = (np.equal, np.less)
NUMPY_OP2_MATH = (np.add, np.subtract, np.multiply, np.true_divide)

if np is not None:
NUMPY_OP2_CMP = (np.equal, np.less)
NUMPY_OP2_MATH = (np.add, np.subtract, np.multiply, np.true_divide)


def float_range(n: int) -> Generator[float, None, None]:
Expand Down Expand Up @@ -53,16 +56,19 @@ def setup(registry_tiny) -> tuple[pint.UnitRegistry, dict[str, Any]]:
return ureg, data


@requires_numpy
def test_finding_meter_getattr(benchmark, setup):
ureg, _ = setup
benchmark(getattr, ureg, "meter")


@requires_numpy
def test_finding_meter_getitem(benchmark, setup):
ureg, _ = setup
benchmark(operator.getitem, ureg, "meter")


@requires_numpy
@pytest.mark.parametrize(
"unit", ["meter", "angstrom", "meter/second", "angstrom/minute"]
)
Expand All @@ -71,19 +77,22 @@ def test_base_units(benchmark, setup, unit):
benchmark(ureg.get_base_units, unit)


@requires_numpy
@pytest.mark.parametrize("key", ALL_ARRAYS)
def test_build_by_mul(benchmark, setup, key):
ureg, data = setup
benchmark(operator.mul, data[key], ureg.meter)


@requires_numpy
@pytest.mark.parametrize("key", ALL_ARRAYS_Q)
@pytest.mark.parametrize("op", OP1 + (np.sqrt, np.square))
def test_op1(benchmark, setup, key, op):
_, data = setup
benchmark(op, data[key])


@requires_numpy
@pytest.mark.parametrize(
"keys",
(
Expand Down

0 comments on commit 43b25d9

Please sign in to comment.