correct error raising tests after more generous numpy coercion method

This commit is contained in:
sneakers-the-rat 2024-05-17 17:55:05 -07:00
parent 8af88d5adc
commit 3a6984f3f0
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D
3 changed files with 15 additions and 5 deletions

View file

@ -154,13 +154,23 @@ class Interface(ABC, Generic[T]):
""" """
Find the interface that should be used for this array based on its input type Find the interface that should be used for this array based on its input type
""" """
matches = [i for i in cls.interfaces() if i.check(array)] # first try and find a non-numpy interface, since the numpy interface
# will try and load the array into memory in its check method
interfaces = cls.interfaces()
non_np_interfaces = [i for i in interfaces if i.__name__ != "NumpyInterface"]
np_interface = [i for i in interfaces if i.__name__ == "NumpyInterface"][0]
matches = [i for i in non_np_interfaces if i.check(array)]
if len(matches) > 1: if len(matches) > 1:
msg = f"More than one interface matches input {array}:\n" msg = f"More than one interface matches input {array}:\n"
msg += "\n".join([f" - {i}" for i in matches]) msg += "\n".join([f" - {i}" for i in matches])
raise ValueError(msg) raise ValueError(msg)
elif len(matches) == 0: elif len(matches) == 0:
raise ValueError(f"No matching interfaces found for input {array}") # now try the numpy interface
if np_interface.check(array):
return np_interface
else:
raise ValueError(f"No matching interfaces found for input {array}")
else: else:
return matches[0] return matches[0]

View file

@ -25,7 +25,7 @@ class NumpyInterface(Interface):
input_types = (ndarray, list) input_types = (ndarray, list)
return_type = ndarray return_type = ndarray
priority = -1 priority = -999
""" """
The numpy interface is usually the interface of last resort. The numpy interface is usually the interface of last resort.
We want to use any more specific interface that we might have, We want to use any more specific interface that we might have,
@ -45,7 +45,7 @@ class NumpyInterface(Interface):
try: try:
_ = np.array(array) _ = np.array(array)
return True return True
except TypeError: except Exception:
return False return False
def before_validation(self, array: Any) -> ndarray: def before_validation(self, array: Any) -> ndarray:

View file

@ -51,7 +51,7 @@ def test_interface_match_error(interfaces):
assert "Interface2" in e assert "Interface2" in e
with pytest.raises(ValueError) as e: with pytest.raises(ValueError) as e:
Interface.match("hey") Interface.match([[1, 2, 3], ["hey"]])
assert "No matching interfaces" in e assert "No matching interfaces" in e
with pytest.raises(ValueError) as e: with pytest.raises(ValueError) as e: