-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. #24867
Conversation
Add a test that shows that a tuple constant (a tuple, where all of its members are also compile-time constants) produces a SyntaxWarning. Then fix this failure.
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
@@ -1211,6 +1211,8 @@ def testAssert2(self): | |||
|
|||
self.check_syntax_warning('assert(x, "msg")', | |||
'assertion is always true') | |||
self.check_syntax_warning('assert(False, "msg")', | |||
'assertion is always true') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add to test that this doesn't warn on assert False, "msg"
.
These tests shouldn't be guarded by @unittest.skipUnless(__debug__, ...)
.
The warning should be issued regardless of __debug__
.
See below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Python/compile.c
Outdated
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || | ||
(s->v.Assert.test->kind == Constant_kind && | ||
PyTuple_Check(s->v.Assert.test->v.Constant.value) && | ||
PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By moving the if (c->c_optimize)
check after these checks, we can warn even when __debug__ == False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Also add some additional unit tests while we are here.
SyntaxWarnings emitted by the compiler when configured to be errors are actually raised as SyntaxError exceptions. Move these tests into their own method and add a test to ensure they are raised. Previously we only tested that they were not raised for a "valid" assertion statement.
The first commit in this patch accidentally added a trailing whitespace. Remove it.
Thank you @tsukasa-au |
Add a test that shows that a tuple constant (a tuple, where all of its
members are also compile-time constants) produces a SyntaxWarning. Then
fix this failure.
https://bugs.python.org/issue43497