Skip to content

Commit 63b4217

Browse files
Review
1 parent dad987f commit 63b4217

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

peps/pep-0679.rst

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ Abstract
1515

1616
This PEP proposes allowing parentheses in the two-argument form of :keyword:`assert`.
1717
The interpreter will reinterpret ``assert (expr, msg)`` as ``assert expr, msg``,
18-
eliminating the common `footgun <https://en.wiktionary.org/wiki/footgun>`_
19-
where such code was previously treated as asserting a two-element :class:`tuple`,
20-
which is always truthy.
18+
eliminating the common pitfall where such code was previously treated as
19+
asserting a two-element :class:`tuple`, which is always truthy.
2120

2221

2322
Motivation
2423
==========
2524

2625
It is a common user mistake when using the form of the :keyword:`assert`
27-
statement that includes the error message to surround it with parentheses [#SO1]_.
26+
statement that includes the error message to surround it with parentheses [#SO1]_ [#RD]_.
2827
This is because many beginners assume :keyword:`!assert` is a function.
2928
The prominent :mod:`unittest` methods, particularly :meth:`~unittest.TestCase.assertTrue`,
3029
also require parentheses around the assertion and message.
@@ -43,7 +42,7 @@ Additionally, some other statements in the language allow parenthesized forms
4342
in one way or another, for example, ``import`` statements
4443
(``from x import (a,b,c)``) or ``del`` statements (``del (a,b,c)``).
4544

46-
Allowing parentheses not only will remove the footgun but also will allow
45+
Allowing parentheses not only will remove the pitfall but also will allow
4746
users and auto-formatters to format long assert statements over multiple lines
4847
in what the authors of this document believe will be a more natural way.
4948
Although it is possible to currently format long :keyword:`assert` statements
@@ -99,13 +98,17 @@ Specification
9998

10099
The formal grammar of the :keyword:`assert` statement will change to:
101100

102-
.. code-block:: bnf
101+
.. code-block::
103102
104-
assert_stmt ::= "assert" expression [',' expression]
105-
| "assert" '(' expression [',' expression] [','] ')'
103+
| 'assert' '(' expression ',' expression [','] ')' &(NEWLINE | ';')
104+
| 'assert' a=expression [',' expression ]
106105
107106
108-
The second case will raise a :exc:`SyntaxWarning` till 3.17.
107+
Where the first line is the new form of the assert statement that allows
108+
parentheses and will raise a :exc:`SyntaxWarning` till 3.17..
109+
The lookahead is needed so statements like ``assert (a, b) <= c, "something"``
110+
are still parsed correctly and to prevent the parser to eagerly capture the
111+
tuple as the full statement.
109112

110113
Optionally, a new "invalid" grammar rule can be added to the parser to
111114
promote the current :exc:`SyntaxWarning` to a :exc:`SyntaxError` in the
@@ -147,6 +150,8 @@ with a ``paren_syntax`` flag::
147150
msg=Constant(value='Error message'),
148151
paren_syntax=1)])
149152

153+
The flag would be removed in 3.18 along with the :exc:`SyntaxWarning`.
154+
150155

151156
Implementing in the compiler
152157
----------------------------
@@ -175,10 +180,9 @@ On the other hand, assert statements of this kind always pass, so they are
175180
effectively not doing anything in user code. The authors of this document think
176181
that this backwards incompatibility nature is beneficial, as it will highlight
177182
these cases in user code while before they will have passed unnoticed. This case
178-
has already raised a :exc:`SyntaxWarning` since Python 3.10, therefore these
179-
cases are expected to be rare, as they require users to ignore existing
180-
syntax warnings. The continued raising of a :exc:`!SyntaxWarning` should
181-
mitigate surprises.
183+
has already raised a :exc:`SyntaxWarning` since Python 3.10, so there has been
184+
a deprecation period of over 5 years.
185+
The continued raising of a :exc:`!SyntaxWarning` should mitigate surprises.
182186

183187
The change will also result in changes to the AST of ``assert (x,y)``,
184188
which currently is:
@@ -263,7 +267,7 @@ the case where they appear in parentheses, which already raises a
263267
:exc:`SyntaxWarning`.
264268

265269
The authors of this PEP believe that adding a completely new syntax will,
266-
first and foremost, not solve the common beginner footgun that this PEP aims to
270+
first and foremost, not solve the common beginner pitfall that this PEP aims to
267271
patch, and will not improve the formatting of assert statements across multiple
268272
lines, which the authors believe the proposed syntax improves.
269273

@@ -286,6 +290,7 @@ Footnotes
286290
=========
287291

288292
.. [#SO1] `StackOverflow: "'assert' statement with or without parentheses" <https://stackoverflow.com/questions/3112171/assert-statement-with-or-without-parentheses>`_
293+
.. [#RD] `/r/python: "Rant: use that second expression in assert! " <https://www.reddit.com/r/Python/comments/1n87g91/rant_use_that_second_expression_in_assert/>`_
289294
.. [#fl8] `flake8: Rule F631 <https://flake8.pycqa.org/en/latest/user/error-codes.html>`_
290295
.. [#pylint] `pylint: assert-on-tuple (W0199) <https://pylint.pycqa.org/en/latest/user_guide/checkers/features.html>`_
291296
.. [#previmp] For the previous parser implementation, see :cpython-pr:`30247`

0 commit comments

Comments
 (0)