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! |
| 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.
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.
| 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.
By moving the if (c->c_optimize) check after these checks, we can warn even when __debug__ == False
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