@@ -1226,11 +1226,33 @@ def test_comparison(self):
12261226 if 1 > 1 : pass
12271227 if 1 <= 1 : pass
12281228 if 1 >= 1 : pass
1229- if 1 is 1 : pass
1230- if 1 is not 1 : pass
1229+ if x is x : pass
1230+ if x is not x : pass
12311231 if 1 in (): pass
12321232 if 1 not in (): pass
1233- if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1 : pass
1233+ if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x : pass
1234+
1235+ def test_comparison_is_literal (self ):
1236+ def check (test , msg = '"is" with a literal' ):
1237+ with self .assertWarnsRegex (SyntaxWarning , msg ):
1238+ compile (test , '<testcase>' , 'exec' )
1239+ with warnings .catch_warnings ():
1240+ warnings .filterwarnings ('error' , category = SyntaxWarning )
1241+ with self .assertRaisesRegex (SyntaxError , msg ):
1242+ compile (test , '<testcase>' , 'exec' )
1243+
1244+ check ('x is 1' )
1245+ check ('x is "thing"' )
1246+ check ('1 is x' )
1247+ check ('x is y is 1' )
1248+ check ('x is not 1' , '"is not" with a literal' )
1249+
1250+ with warnings .catch_warnings ():
1251+ warnings .filterwarnings ('error' , category = SyntaxWarning )
1252+ compile ('x is None' , '<testcase>' , 'exec' )
1253+ compile ('x is False' , '<testcase>' , 'exec' )
1254+ compile ('x is True' , '<testcase>' , 'exec' )
1255+ compile ('x is ...' , '<testcase>' , 'exec' )
12341256
12351257 def test_binary_mask_ops (self ):
12361258 x = 1 & 1
@@ -1520,9 +1542,11 @@ def test_paren_evaluation(self):
15201542 self .assertEqual (16 // (4 // 2 ), 8 )
15211543 self .assertEqual ((16 // 4 ) // 2 , 2 )
15221544 self .assertEqual (16 // 4 // 2 , 2 )
1523- self .assertTrue (False is (2 is 3 ))
1524- self .assertFalse ((False is 2 ) is 3 )
1525- self .assertFalse (False is 2 is 3 )
1545+ x = 2
1546+ y = 3
1547+ self .assertTrue (False is (x is y ))
1548+ self .assertFalse ((False is x ) is y )
1549+ self .assertFalse (False is x is y )
15261550
15271551 def test_matrix_mul (self ):
15281552 # This is not intended to be a comprehensive test, rather just to be few
0 commit comments