Forbid subclassing _TensorBase directly (#125558)

As per title.
This ensures that all the places where we assume the method defined in _tensor.py do exist.

BC-Breaking: This is bc-breaking as the user cannot subclass this private class anymore.
You should replace any use of _TensorBase to Tensor.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/125558
Approved by: https://github.com/ezyang
This commit is contained in:
albanD
2024-05-08 20:29:25 +00:00
committed by PyTorch MergeBot
parent afea237935
commit 19a9de114a
3 changed files with 28 additions and 15 deletions

View File

@ -769,7 +769,7 @@ $1: f32[] = torch._ops.my_lib.weird.default(['None', '$0'])""",
# test all sequence types are permissible returns
for list_type in (list, tuple):
class A(torch._C.TensorBase):
class A(torch.Tensor):
@staticmethod
def __new__(cls, elem):
return torch.Tensor._make_subclass(cls, elem, elem.requires_grad)
@ -789,7 +789,7 @@ $1: f32[] = torch._ops.my_lib.weird.default(['None', '$0'])""",
def test_invalid_ret(self) -> None:
# test invalid return gets reasonable error message
class A(torch._C.TensorBase):
class A(torch.Tensor):
@staticmethod
def __new__(cls, elem):
return torch.Tensor._make_subclass(cls, elem, elem.requires_grad)