Skip to content

Commit 872d972

Browse files
zou3519pytorchmergebot
authored andcommitted
[custom_op] better error message on no returns (#129896)
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 Pull Request resolved: #129896 Approved by: https://github.com/yushangdi
1 parent aa0352c commit 872d972

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
@@ -187,6 +187,9 @@ def parse_return(annotation, error_fn):
187187
if annotation is None:
188188
return "()"
189189

190+
if annotation is inspect.Parameter.empty:
191+
error_fn("No return type annotation was provided. Please add one.")
192+
190193
origin = typing.get_origin(annotation)
191194
if origin is not tuple:
192195
if annotation not in SUPPORTED_RETURN_TYPES.keys():

0 commit comments

Comments
 (0)