mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
gh-103492: Clarify SyntaxWarning with literal comparison (#103493)
This commit is contained in:
parent
79ae019164
commit
ae25855045
4 changed files with 34 additions and 16 deletions
|
@ -236,12 +236,9 @@ class TokenTests(unittest.TestCase):
|
|||
check(f"[{num}for x in ()]")
|
||||
check(f"{num}spam", error=True)
|
||||
|
||||
with self.assertWarnsRegex(SyntaxWarning, r'invalid \w+ literal'):
|
||||
compile(f"{num}is x", "<testcase>", "eval")
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings('ignore', '"is" with a literal',
|
||||
SyntaxWarning)
|
||||
with self.assertWarnsRegex(SyntaxWarning,
|
||||
r'invalid \w+ literal'):
|
||||
compile(f"{num}is x", "<testcase>", "eval")
|
||||
warnings.simplefilter('error', SyntaxWarning)
|
||||
with self.assertRaisesRegex(SyntaxError,
|
||||
r'invalid \w+ literal'):
|
||||
|
@ -1467,14 +1464,22 @@ class GrammarTests(unittest.TestCase):
|
|||
if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass
|
||||
|
||||
def test_comparison_is_literal(self):
|
||||
def check(test, msg='"is" with a literal'):
|
||||
def check(test, msg):
|
||||
self.check_syntax_warning(test, msg)
|
||||
|
||||
check('x is 1')
|
||||
check('x is "thing"')
|
||||
check('1 is x')
|
||||
check('x is y is 1')
|
||||
check('x is not 1', '"is not" with a literal')
|
||||
check('x is 1', '"is" with \'int\' literal')
|
||||
check('x is "thing"', '"is" with \'str\' literal')
|
||||
check('1 is x', '"is" with \'int\' literal')
|
||||
check('x is y is 1', '"is" with \'int\' literal')
|
||||
check('x is not 1', '"is not" with \'int\' literal')
|
||||
check('x is not (1, 2)', '"is not" with \'tuple\' literal')
|
||||
check('(1, 2) is not x', '"is not" with \'tuple\' literal')
|
||||
|
||||
check('None is 1', '"is" with \'int\' literal')
|
||||
check('1 is None', '"is" with \'int\' literal')
|
||||
|
||||
check('x == 3 is y', '"is" with \'int\' literal')
|
||||
check('x == "thing" is y', '"is" with \'str\' literal')
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('error', SyntaxWarning)
|
||||
|
@ -1482,6 +1487,10 @@ class GrammarTests(unittest.TestCase):
|
|||
compile('x is False', '<testcase>', 'exec')
|
||||
compile('x is True', '<testcase>', 'exec')
|
||||
compile('x is ...', '<testcase>', 'exec')
|
||||
compile('None is x', '<testcase>', 'exec')
|
||||
compile('False is x', '<testcase>', 'exec')
|
||||
compile('True is x', '<testcase>', 'exec')
|
||||
compile('... is x', '<testcase>', 'exec')
|
||||
|
||||
def test_warn_missed_comma(self):
|
||||
def check(test):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue