Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f7f0ac

Browse files
committed
Improve syntax error for not after a unary operator
1 parent ac6521a commit 6f7f0ac

File tree

3 files changed

+766
-622
lines changed

3 files changed

+766
-622
lines changed

Grammar/python.gram

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ term[expr_ty]:
795795
| a=term '//' b=factor { _PyAST_BinOp(a, FloorDiv, b, EXTRA) }
796796
| a=term '%' b=factor { _PyAST_BinOp(a, Mod, b, EXTRA) }
797797
| a=term '@' b=factor { CHECK_VERSION(expr_ty, 5, "The '@' operator is", _PyAST_BinOp(a, MatMult, b, EXTRA)) }
798+
| invalid_factor
798799
| factor
799800

800801
factor[expr_ty] (memo):
@@ -1419,3 +1420,5 @@ invalid_conversion_character:
14191420

14201421
invalid_arithmetic:
14211422
| sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
1423+
invalid_factor:
1424+
| ('+' | '-' | '~') a='not' b=factor { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }

Lib/test/test_syntax.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,15 @@
17221722
Traceback (most recent call last):
17231723
SyntaxError: 'not' after an operator must be parenthesized
17241724
1725-
>>> 3 ~ not 3
1725+
>>> + not 3
1726+
Traceback (most recent call last):
1727+
SyntaxError: 'not' after an operator must be parenthesized
1728+
1729+
>>> - not 3
1730+
Traceback (most recent call last):
1731+
SyntaxError: 'not' after an operator must be parenthesized
1732+
1733+
>>> ~ not 3
17261734
Traceback (most recent call last):
17271735
SyntaxError: 'not' after an operator must be parenthesized
17281736
@@ -1735,6 +1743,10 @@
17351743
Traceback (most recent call last):
17361744
SyntaxError: invalid syntax
17371745
1746+
>>> not + 1 +
1747+
Traceback (most recent call last):
1748+
SyntaxError: invalid syntax
1749+
17381750
Corner-cases that used to fail to raise the correct error:
17391751
17401752
>>> def f(*, x=lambda __debug__:0): pass

0 commit comments

Comments
 (0)