bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. (GH-24867)

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

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.

* Make SyntaxWarnings also work when "optimized".

* Split tests for SyntaxWarning to SyntaxError conversion

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.
This commit is contained in:
tsukasa-au 2021-03-16 22:14:41 +11:00 committed by GitHub
parent 1330338583
commit a8ef4572a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View file

@ -3355,10 +3355,12 @@ compiler_assert(struct compiler *c, stmt_ty s)
{
basicblock *end;
if (c->c_optimize)
return 1;
if (s->v.Assert.test->kind == Tuple_kind &&
asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
/* Always emit a warning if the test is a non-zero length tuple */
if ((s->v.Assert.test->kind == Tuple_kind &&
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))
{
if (!compiler_warn(c, "assertion is always true, "
"perhaps remove parentheses?"))
@ -3366,6 +3368,8 @@ compiler_assert(struct compiler *c, stmt_ty s)
return 0;
}
}
if (c->c_optimize)
return 1;
end = compiler_new_block(c);
if (end == NULL)
return 0;