Capture TypeError in CONTAINS_OP (#161069)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/161069
Approved by: https://github.com/anijain2305
This commit is contained in:
Guilherme Leobas
2025-09-04 00:47:51 +00:00
committed by PyTorch MergeBot
parent 66f3b4a682
commit 480c739112
7 changed files with 10 additions and 8 deletions

View File

@ -1756,7 +1756,6 @@ class FunctionTests(torch._dynamo.test_case.TestCase):
return a + b
return a - b
@unittest.expectedFailure
@make_test
def test_set_in_frozenset(x):
var = set("abc")

View File

@ -657,7 +657,6 @@ class FrozensetTests(_FrozensetBase, _BaseSetTests):
class SetTests(_SetBase, _BaseSetTests):
thetype = set
@unittest.expectedFailure
def test_in_frozenset(self):
super().test_in_frozenset()
@ -668,13 +667,11 @@ class UserDefinedSetTests(_SetBase, _BaseSetTests):
thetype = CustomSet
@unittest.expectedFailure
def test_in_frozenset(self):
super().test_in_frozenset()
@unittest.expectedFailure
def test_equality(self):
super().test_in_frozenset()
super().test_equality()
class UserDefinedFrozensetTests(_FrozensetBase, _BaseSetTests):
@ -683,7 +680,6 @@ class UserDefinedFrozensetTests(_FrozensetBase, _BaseSetTests):
thetype = CustomFrozenset
@unittest.expectedFailure
def test_in_frozenset(self):
super().test_in_frozenset()

View File

@ -3182,9 +3182,16 @@ class InstructionTranslatorBase(
op = inst.argval
try:
self.push(right.call_method(self, "__contains__", [left], {}))
except Unsupported as excp: # object doesn't support __contains__
except (
# right.__contains__ can raise TypeError
exc.ObservedTypeError,
# Ideally we should only capture TypeError here but some VTs don't
# implement hasattr(vt, "__contains__") entirely
Unsupported,
) as excp: # object doesn't support __contains__
# Use __iter__ as fallback
excp.remove_from_stats()
if isinstance(excp, Unsupported):
excp.remove_from_stats()
self.push(
self.inline_user_function_return(
VariableTracker.build(self, impl_CONTAINS_OP_fallback),