[opaque obj] Error for torch.library.custom_op infer_schema (#163277)

Unsure how we can get infer_schema to infer the scriptObject type from just the type annotation, so for now will just error clearly and ask users to specify a schema.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/163277
Approved by: https://github.com/zou3519
ghstack dependencies: #163279
This commit is contained in:
angelayi
2025-10-07 15:36:00 -07:00
committed by PyTorch MergeBot
parent 56ef7743fc
commit 2bb4e6876c
4 changed files with 36 additions and 6 deletions

View File

@ -35,6 +35,7 @@ from torch._library.fake_profile import (
TensorMetadata,
)
from torch._library.infer_schema import tuple_to_list
from torch._library.opaque_object import make_opaque, OpaqueType
from torch._utils_internal import get_file_path_2 # @manual
from torch.fx.experimental.symbolic_shapes import ShapeEnv
from torch.testing._internal import custom_op_db
@ -901,6 +902,8 @@ class TestCustomOp(CustomOpTestCaseBase):
return [torch.tensor(3)]
if typ == Optional[torch.types.Number]:
return [None, 2.718]
if typ == OpaqueType:
return [make_opaque("moo")]
origin = typing.get_origin(typ)
if origin is Union:
args = typing.get_args(typ)