Add Any return annotation to __getattr__ methods that return a union of types. (#150204)

Adds an `Any` return type annotation to `__getattr__` methods in `torch/_ops.py` that return a union of types. Attribute access returning a union of types can cause issues downstream because consumers would need to handle all of the possible types to make the type checker happy. This doesn't seem to matter today for mypy, presumably because `Any` is always inferred when a return type annotation is missing, but it still makes explicit what mypy is already doing implicitly.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150204
Approved by: https://github.com/malfet
This commit is contained in:
Rebecca Chen 2025-04-02 05:25:03 +00:00 committed by PyTorch MergeBot
parent dee016ceb7
commit c65de03196

View File

@ -1086,7 +1086,7 @@ class OpOverloadPacket:
for overload_name in self._overload_names
}
def __getattr__(self, key):
def __getattr__(self, key) -> Any:
# It is not a valid op_name when __file__ is passed in
if key == "__file__":
return "torch.ops"
@ -1246,7 +1246,7 @@ class _OpNamespace(types.ModuleType):
def __iter__(self):
return iter(self._dir)
def __getattr__(self, op_name):
def __getattr__(self, op_name) -> Any:
# It is not a valid op_name when __file__ is passed in
if op_name == "__file__":
return "torch.ops"