Skip to content
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

Merged
merged 5 commits into from
Mar 16, 2021
Merged

bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. #24867

merged 5 commits into from
Mar 16, 2021

Conversation

tsukasa-au
Copy link
Contributor

@tsukasa-au tsukasa-au commented Mar 15, 2021

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

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.
@tsukasa-au tsukasa-au requested a review from markshannon as a code owner March 15, 2021 07:48
@the-knights-who-say-ni
Copy link

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 Missing

Our records indicate the following people have not signed the CLA:

@tsukasa-au

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
before our records are updated.

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')
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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

Copy link
Contributor Author

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.
@markshannon
Copy link
Member

Thank you @tsukasa-au

@markshannon markshannon merged commit a8ef457 into python:master Mar 16, 2021
@tsukasa-au tsukasa-au deleted the fix-issue-43497 branch March 17, 2021 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants