[torchbind] fix error message when attr is a real tensor. (#151944)

Summary: Previously, when attr is defined, "if attr" will try to evaluate the data of attr, which is not intendended and we get a ugly error stack if the attr is not evaluable (like a fake tensor) before the callable(attr) check.

Test Plan: Existing tests.

Reviewed By: yushangdi, henryoier

Differential Revision: D73460905

Pull Request resolved: https://github.com/pytorch/pytorch/pull/151944
Approved by: https://github.com/yushangdi
This commit is contained in:
Yidi Wu
2025-04-23 17:32:07 +00:00
committed by PyTorch MergeBot
parent 9344da8bd1
commit 5f63789dd2

View File

@ -155,7 +155,7 @@ def maybe_to_fake_obj(
for name in x._method_names(): # type: ignore[attr-defined]
attr = getattr(fake_x, name, None)
if attr:
if attr is not None:
if not callable(attr):
raise RuntimeError(f"Expect {name} to be a callable but got {attr}.")