Skip to content

bpo-46820: Refactor tests for ambiguous end of numerical literal #31494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 25 additions & 89 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,99 +214,35 @@ def test_bad_numerical_literals(self):
check("1e+", "invalid decimal literal")

def test_end_of_numerical_literals(self):
def check(test):
with self.assertWarns(DeprecationWarning):
compile(test, "<testcase>", "eval")

def check_error(test):
with warnings.catch_warnings(record=True) as w:
with self.assertRaises(SyntaxError):
compile(test, "<testcase>", "eval")
self.assertEqual(w, [])

check_error("0xfand x")
check("0o7and x")
check("0b1and x")
check("9and x")
check("0and x")
check("1.and x")
check("1e3and x")
check("1jand x")

check("0xfor x")
check("0o7or x")
check("0b1or x")
check("9or x")
check_error("0or x")
check("1.or x")
check("1e3or x")
check("1jor x")

check("0xfin x")
check("0o7in x")
check("0b1in x")
check("9in x")
check("0in x")
check("1.in x")
check("1e3in x")
check("1jin x")

check("0xfnot in x")
check("0o7not in x")
check("0b1not in x")
check("9not in x")
check("0not in x")
check("1.not in x")
check("1e3not in x")
check("1jnot in x")

with warnings.catch_warnings():
warnings.simplefilter('ignore', SyntaxWarning)
check("0xfis x")
check("0o7is x")
check("0b1is x")
check("9is x")
check("0is x")
check("1.is x")
check("1e3is x")
check("1jis x")

check("0xfif x else y")
check("0o7if x else y")
check("0b1if x else y")
check("9if x else y")
check("0if x else y")
check("1.if x else y")
check("1e3if x else y")
check("1jif x else y")

check_error("x if 0xfelse y")
check("x if 0o7else y")
check("x if 0b1else y")
check("x if 9else y")
check("x if 0else y")
check("x if 1.else y")
check("x if 1e3else y")
check("x if 1jelse y")
def check(test, error=False):
with self.subTest(expr=test):
if error:
with warnings.catch_warnings(record=True) as w:
with self.assertRaises(SyntaxError):
compile(test, "<testcase>", "eval")
self.assertEqual(w, [])
else:
with self.assertWarns(DeprecationWarning):
compile(test, "<testcase>", "eval")

for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is now a better time to suggest adding an underscore literal 1_000?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are too many different use cases with underscores: 0x_f, 0x1_f, 0o_7, 0o1_7, 0b_1, 0b1_1, 1., 1_0., 1.0_0, etc. And none of them is related because an underscore cannot ends the numeric literal, and it does not affect interpretation of other characters.

It will increase test coverage.

compile(num, "<testcase>", "eval")
check(f"{num}and x", error=(num == "0xf"))
check(f"{num}or x", error=(num == "0"))
check(f"{num}in x")
check(f"{num}not in x")
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '"is" with a literal',
SyntaxWarning)
check(f"{num}is x")
check(f"{num}if x else y")
check(f"x if {num}else y", error=(num == "0xf"))
check(f"[{num}for x in ()]")
check(f"{num}spam", error=True)

check("[0x1ffor x in ()]")
check("[0x1for x in ()]")
check("[0xfor x in ()]")
check("[0o7for x in ()]")
check("[0b1for x in ()]")
check("[9for x in ()]")
check("[1.for x in ()]")
check("[1e3for x in ()]")
check("[1jfor x in ()]")

check_error("0xfspam")
check_error("0o7spam")
check_error("0b1spam")
check_error("9spam")
check_error("0spam")
check_error("1.spam")
check_error("1e3spam")
check_error("1jspam")

def test_string_literals(self):
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
Expand Down