提交 cbf91122 authored 作者: Ricardo's avatar Ricardo 提交者: Brandon T. Willard

Make input mismatch TypeError in make_node more readable

上级 620edabe
...@@ -225,7 +225,15 @@ class Op(MetaObject): ...@@ -225,7 +225,15 @@ class Op(MetaObject):
) )
if not all(inp.type == it for inp, it in zip(inputs, self.itypes)): if not all(inp.type == it for inp, it in zip(inputs, self.itypes)):
raise TypeError( 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]) return Apply(self, inputs, [o() for o in self.otypes])
......
...@@ -12,7 +12,7 @@ from aesara.graph.op import COp, Op ...@@ -12,7 +12,7 @@ from aesara.graph.op import COp, Op
from aesara.graph.type import Generic, Type from aesara.graph.type import Generic, Type
from aesara.graph.utils import MethodNotDefined, TestValueError from aesara.graph.utils import MethodNotDefined, TestValueError
from aesara.tensor.math import log 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): def as_variable(x):
...@@ -340,3 +340,16 @@ def test_get_test_values_exc(): ...@@ -340,3 +340,16 @@ def test_get_test_values_exc():
with pytest.raises(TestValueError): with pytest.raises(TestValueError):
x = vector() x = vector()
assert op.get_test_values(x) == [] 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())
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论