Remove property from python_type function (#152900)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/152900
Approved by: https://github.com/amjames, https://github.com/anijain2305
ghstack dependencies: #153070
This commit is contained in:
Guilherme Leobas 2025-05-12 19:38:36 -03:00 committed by PyTorch MergeBot
parent f67eb6f8c5
commit a4459cd4e3
3 changed files with 20 additions and 1 deletions

View File

@ -22,6 +22,15 @@ class CustomException(Exception):
...
class CustomExceptionMeta(type):
def __instancecheck__(cls, instance):
return True
class CustomExceptionWithInstanceCheck(Exception, metaclass=CustomExceptionMeta):
...
class CustomExceptionWithArgs(Exception):
def __init__(self, a, b=None):
self.a = a
@ -149,6 +158,14 @@ class ExceptionTests(torch._dynamo.test_case.TestCase):
out = f(inp)
self.assertTrue(torch.equal(out, inp + 1))
@make_dynamo_test
def test_isinstance_CustomException(self):
assert isinstance(CustomException, type)
assert not isinstance(CustomException(), type)
C = CustomExceptionWithInstanceCheck
assert isinstance(C, C)
assert isinstance(C(), C)
@make_dynamo_test
def test_propagate_exception_inside_ctx_manager(self):
@contextlib.contextmanager

View File

@ -1805,6 +1805,9 @@ class BuiltinVariable(VariableTracker):
isinstance_type.__class__.__instancecheck__(isinstance_type, arg.value)
)
if isinstance(arg, variables.UserDefinedExceptionClassVariable):
return ConstantVariable.create(isinstance(arg_type, isinstance_type))
isinstance_type_tuple: tuple[type, ...]
if isinstance(isinstance_type, type) or callable(
# E.g. isinstance(obj, typing.Sequence)

View File

@ -707,7 +707,6 @@ class UserDefinedExceptionClassVariable(UserDefinedClassVariable):
def fn(self):
return self.value
@property
def python_type(self):
return self.value