@@ -2234,7 +2234,8 @@ def testfunc(n):
22342234 x = 0
22352235 for _ in range (n ):
22362236 # One of the classes is unknown, but it comes
2237- # after a known class, so we can narrow to True
2237+ # after a known class, so we can narrow to True and
2238+ # remove the isinstance call.
22382239 y = isinstance (42 , (int , eval ('str' )))
22392240 if y :
22402241 x += 1
@@ -2246,16 +2247,17 @@ def testfunc(n):
22462247 uops = get_opnames (ex )
22472248 self .assertNotIn ("_CALL_ISINSTANCE" , uops )
22482249 self .assertNotIn ("_TO_BOOL_BOOL" , uops )
2249- self .assertNotIn ("_GUARD_IS_FALSE_POP " , uops )
2250+ self .assertNotIn ("_GUARD_IS_TRUE_POP " , uops )
22502251 self .assertIn ("_BUILD_TUPLE" , uops )
22512252 self .assertIn ("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW" , uops )
22522253
22532254 def test_call_isinstance_tuple_of_classes_true_unknown_2 (self ):
22542255 def testfunc (n ):
22552256 x = 0
22562257 for _ in range (n ):
2257- # One of the classes is unknown, so we can't narrow
2258- # to True or False, only bool
2258+ # We can narrow to True, but since the unknown class comes
2259+ # first and could potentially trigger an __instancecheck__,
2260+ # we can't remove the isinstance call.
22592261 y = isinstance (42 , (eval ('str' ), int ))
22602262 if y :
22612263 x += 1
@@ -2267,14 +2269,13 @@ def testfunc(n):
22672269 uops = get_opnames (ex )
22682270 self .assertIn ("_CALL_ISINSTANCE" , uops )
22692271 self .assertNotIn ("_TO_BOOL_BOOL" , uops )
2270- self .assertIn ("_GUARD_IS_TRUE_POP" , uops )
2272+ self .assertNotIn ("_GUARD_IS_TRUE_POP" , uops )
22712273
22722274 def test_call_isinstance_tuple_of_classes_true_unknown_3 (self ):
22732275 def testfunc (n ):
22742276 x = 0
22752277 for _ in range (n ):
2276- # One of the classes is unknown, so we can't narrow
2277- # to True or False, only bool
2278+ # We can only narrow to bool here
22782279 y = isinstance (42 , (str , eval ('int' )))
22792280 if y :
22802281 x += 1
@@ -2292,8 +2293,7 @@ def test_call_isinstance_tuple_of_classes_true_unknown_4(self):
22922293 def testfunc (n ):
22932294 x = 0
22942295 for _ in range (n ):
2295- # One of the classes is unknown, so we can't narrow
2296- # to True or False, only bool
2296+ # We can only narrow to bool here
22972297 y = isinstance (42 , (eval ('int' ), str ))
22982298 if y :
22992299 x += 1
0 commit comments