Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make input mismatch TypeError in make_node more readable #655

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion aesara/graph/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,15 @@ def make_node(self, *inputs: Variable) -> Apply:
)
if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
raise TypeError(
f"We expected inputs of types '{str(self.itypes)}' but got types '{str([inp.type for inp in inputs])}'"
f"Invalid input types for Op {self}:\n"
+ "\n".join(
f"Input {i}/{len(inputs)}: Expected {inp}, got {out}"
for i, (inp, out) in enumerate(
zip(self.itypes, (inp.type for inp in inputs)),
start=1,
)
if inp != out
)
)
return Apply(self, inputs, [o() for o in self.otypes])

Expand Down
15 changes: 14 additions & 1 deletion tests/graph/test_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from aesara.graph.type import Generic, Type
from aesara.graph.utils import MethodNotDefined, TestValueError
from aesara.tensor.math import log
from aesara.tensor.type import dmatrix, vector
from aesara.tensor.type import dmatrix, dscalar, dvector, vector


def as_variable(x):
Expand Down Expand Up @@ -340,3 +340,16 @@ def test_get_test_values_exc():
with pytest.raises(TestValueError):
x = vector()
assert op.get_test_values(x) == []


def test_op_invalid_input_types():
class TestOp(aesara.graph.op.Op):
itypes = [dvector, dvector, dvector]
otypes = [dvector]

def perform(self, node, inputs, outputs):
pass

msg = r"^Invalid input types for Op TestOp:\nInput 2/3: Expected TensorType\(float64, vector\)"
with pytest.raises(TypeError, match=msg):
TestOp()(dvector(), dscalar(), dvector())