summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_grammar.py
diff options
context:
space:
mode:
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>2023-04-24 21:42:57 (GMT)
committerGitHub <noreply@github.com>2023-04-24 21:42:57 (GMT)
commitae25855045e8f19f4715c9b2c02cbcd81e7f6f95 (patch)
treef3530704fce4ac964f45d4ad2623ad3ea62338a7 /Lib/test/test_grammar.py
parent79ae019164eeb6b94118bc17bc1e937405684c75 (diff)
downloadcpython-ae25855045e8f19f4715c9b2c02cbcd81e7f6f95.zip
cpython-ae25855045e8f19f4715c9b2c02cbcd81e7f6f95.tar.gz
cpython-ae25855045e8f19f4715c9b2c02cbcd81e7f6f95.tar.bz2
gh-103492: Clarify SyntaxWarning with literal comparison (#103493)
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r--Lib/test/test_grammar.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index ced9000..ee105a3 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -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):