Skip to content

Commit

Permalink
Patch the actual numpy import, not just the module
Browse files Browse the repository at this point in the history
  • Loading branch information
jagerber48 committed Nov 6, 2024
1 parent bea4487 commit ced0caa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
59 changes: 59 additions & 0 deletions tests/test_no_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest
from unittest.mock import patch


orig_import = __import__


def no_numpy_import(*args, **kwargs):
print(args[0])
if args and args[0].startswith("numpy"):
raise ImportError

Check warning on line 11 in tests/test_no_numpy.py

View check run for this annotation

Codecov / codecov/patch

tests/test_no_numpy.py#L11

Added line #L11 was not covered by tests
elif "name" in kwargs and kwargs["name"].startswith("numpy"):
raise ImportError

Check warning on line 13 in tests/test_no_numpy.py

View check run for this annotation

Codecov / codecov/patch

tests/test_no_numpy.py#L13

Added line #L13 was not covered by tests
else:
return orig_import(*args, **kwargs)


with patch("builtins.__import__", no_numpy_import):
from uncertainties import (
correlated_values,
correlated_values_norm,
correlation_matrix,
ufloat,
)


def test_no_numpy():
nom_values = [1, 2, 3]
std_devs = [0.1, 0.2, 0.3]
cov = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlated_values(nom_values, cov)

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlated_values_norm(
list(zip(nom_values, std_devs)),
cov,
)

x = ufloat(1, 0.1)
y = ufloat(2, 0.2)
z = ufloat(3, 0.3)

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlation_matrix([x, y, z])
45 changes: 1 addition & 44 deletions tests/test_uncertainties.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import copy
import math
import random # noqa
from unittest.mock import patch

import pytest

import uncertainties.core as uncert_core
from uncertainties.core import ufloat, AffineScalarFunc, ufloat_fromstr
from uncertainties import (
umath,
correlated_values,
correlated_values_norm,
correlation_matrix,
)
from uncertainties import umath
from helpers import (
power_special_cases,
power_all_cases,
Expand Down Expand Up @@ -1321,39 +1314,3 @@ def test_correlated_values_correlation_mat():
numpy.array(cov_mat),
numpy.array(uncert_core.covariance_matrix([x2, y2, z2])),
)


@patch("uncertainties.core.numpy", None)
def test_no_numpy():
nom_values = [1, 2, 3]
std_devs = [0.1, 0.2, 0.3]
cov = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlated_values(nom_values, cov)

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlated_values_norm(
list(zip(nom_values, std_devs)),
cov,
)

x = ufloat(1, 0.1)
y = ufloat(2, 0.2)
z = ufloat(3, 0.3)

with pytest.raises(
NotImplementedError,
match="not able to import numpy",
):
_ = correlation_matrix([x, y, z])

0 comments on commit ced0caa

Please sign in to comment.