Skip to content

Commit d3ac32d

Browse files
committed
[custom_op] better error message on no returns
I run into this a lot. I can imagine that it would look opaque to users, so made it more friendly Old error message: "ValueError: infer_schema(func): Return has unsupported type <class 'inspect._empty'>." Test Plan: - new tests [ghstack-poisoned]
1 parent 9ae78a5 commit d3ac32d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

test/test_custom_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,15 @@ def blah8(x, *, y=1):
595595
def blah9(x, *, y):
596596
pass
597597

598+
def test_infer_schema_no_return(self):
599+
with self.assertRaisesRegex(
600+
ValueError, "No return type annotation was provided. Please add one."
601+
):
602+
603+
@torch.library.custom_op("mylib::foo", mutates_args={})
604+
def foo(x: torch.Tensor, y: int):
605+
return x * y
606+
598607
def test_infer_schema_supported(self):
599608
def a(x: Tensor) -> Tensor:
600609
return torch.empty([])

torch/_library/infer_schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def parse_return(annotation, error_fn):
176176
if annotation is None:
177177
return "()"
178178

179+
if annotation is inspect.Parameter.empty:
180+
error_fn("No return type annotation was provided. Please add one.")
181+
179182
origin = typing.get_origin(annotation)
180183
if origin is not tuple:
181184
if annotation not in SUPPORTED_RETURN_TYPES.keys():

0 commit comments

Comments
 (0)