This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: not operator expression raising a syntax error
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, benjamin.peterson, candide, ezio.melotti, iritkatriel, martin.panter, mrabarnett, pablogsal, scoder, serhiy.storchaka, steven.daprano
Priority: normal Keywords: patch

Created on 2015-07-11 15:23 by candide, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 28170 open pablogsal, 2021-09-04 21:33
Messages (10)
msg246606 - (view) Author: candide (candide) Date: 2015-07-11 15:23
Expressions such as

a + not b
a * not b
+ not b
- not b

raise a SyntaxError, for instance :


>>> 0 + not 0
  File "<stdin>", line 1
    0 + not 0
          ^
SyntaxError: invalid syntax
>>> - not 0
  File "<stdin>", line 1
    - not 0
        ^
SyntaxError: invalid syntax
>>>

if the not expression is wrapped in parenthesis, expected evaluation occurs:


>>> - not 0
  File "<stdin>", line 1
    - not 0
        ^
SyntaxError: invalid syntax
>>> 0 + (not 0)
1
>>> - (not 0)
-1
>>>



The problem has been first submitted in comp.lang.python :

https://groups.google.com/forum/?hl=fr#!topic/comp.lang.python/iZiBs3tcuak


suggesting  a bug report.
msg246607 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-07-11 16:18
"not not 0" is compiled successful, while "+ not 0", "- not 0", and "~ not 0" are rejected.
msg246608 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2015-07-11 16:23
On Sat, Jul 11, 2015 at 03:23:53PM +0000, candide wrote:
> 
> New submission from candide:
> 
> Expressions such as
> 
> a + not b
> a * not b
> + not b
> - not b
> 
> raise a SyntaxError, for instance :
> 
> 
> >>> 0 + not 0
>   File "<stdin>", line 1
>     0 + not 0
>           ^
> SyntaxError: invalid syntax

That has been invalid syntax since Python 1.5, if not older. I don't 
think that it needs to be changed.

[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 0 + not 0
  File "<stdin>", line 1
    0 + not 0
          ^
SyntaxError: invalid syntax
msg246611 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2015-07-11 18:43
It looks like perfectly valid syntax to me and I cannot see why it should be semantically rejected. I disagree that it shouldn't be changed. I think it's a (minor) bug that should eventually get fixed.
msg246612 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2015-07-11 19:07
"not" has a lower priority than unary "-"; this:

    not a < b

is parsed as:

    not (a < b)

How would you parse:

    0 + not 0 + 0

?

Would it be parsed as:

    0 + not (0 + 0)

?

Similar remarks could apply to "yield":

    0 + yield 0

which is also a syntax error.
msg246613 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2015-07-11 19:13
Hmm, right. I see your point and also the analogy with "yield". I could live with (maybe) giving it a better error message suggesting to use parentheses for clarity and otherwise keeping it a SyntaxError.
msg246619 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-07-12 00:11
Funny, I ran into this one or two days ago, when refactoring some code that used the bitwise exclusive-or operator, since there is no boolean exclusive or:

-if (x == a) ^ (y != b): ...
+aa = x == a
+bb = y == b
+if aa ^ not bb: ...

It is fairly clear what I wanted to do above, but with “is not” you would have to avoid ambiguity:

>>> "spam" is not "ham"  # Using “is not” operator
True
>>> "spam" is (not "ham")  # Two distinct operators
False

I think it would be too complicated to make unary “not” bind more tightly than “and” in some cases but not in others. How would you handle these cases?

a + not b and c
a ** not b ** c
a is not not b

The way I see it, there is no equivalent problem with the other unary operators: arithmetic (+, -), bitwise (~), and “await”. Await has higher priority than all other binary operators, so no problem there. The other three are equal with the highest priority binary operator, exponentiation (**):

>>> 2 ** -1 ** 2  # Evaluated right to left
0.5
>>> 2 ** (-1) ** 2  # Negation first
2
>>> (2 ** -1) ** 2  # Left-hand exponentiation first
0.25

BTW, in the operator precedence table <https://docs.python.org/dev/reference/expressions.html#operator-precedence>, I think exponentiation should be in the same priority group as the arithmetic and bitwise unary operations. At the moment it says exponentiation has higher priority, but has a footnote saying this is reversed on the right-hand side of an exponentiation. This is unclear when applied to my example above.
msg246620 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-07-12 00:23
BTW “yield” is not a fair comparison because its syntax is even stranger (due to originally being a “yield” statement):

def g():
    x = yield + 1  # Yield the value +1
    y = (yield) + 1  # Add 1 after yielding
    return (yield)  # Mandatory brackets
msg401055 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-04 18:37
Reproduced on 3.11:

>>> 0 + not 0
  File "<stdin>", line 1
    0 + not 0
        ^^^
SyntaxError: invalid syntax
>>> - not 0
  File "<stdin>", line 1
    - not 0
      ^^^
SyntaxError: invalid syntax
msg401063 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-04 21:19
I think the best outcome here is to refine the syntax error. Making it behave a bit better is going to be quite a pain because of how unary "-" and "not" work on the priority level in the grammar.

I also don't think that facilitating the concatenation of operators without parentheses is a good idea (for readability reasons). 

I will prepare a PR to improve the syntax error
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68800
2021-09-05 01:21:27arobergesetnosy: + aroberge
2021-09-04 21:33:17pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26599
2021-09-04 21:19:01pablogsalsetmessages: + msg401063
2021-09-04 18:37:30iritkatrielsetnosy: + pablogsal, iritkatriel

messages: + msg401055
versions: + Python 3.11, - Python 3.5, Python 3.6
2016-01-03 23:51:24ezio.melottisetnosy: + ezio.melotti

versions: + Python 3.5, Python 3.6, - Python 3.4
2015-07-12 00:23:44martin.pantersetmessages: + msg246620
2015-07-12 00:11:54martin.pantersetmessages: + msg246619
2015-07-11 21:35:05martin.pantersetnosy: + martin.panter
2015-07-11 19:13:59scodersetmessages: + msg246613
2015-07-11 19:07:25mrabarnettsetnosy: + mrabarnett
messages: + msg246612
2015-07-11 18:43:39scodersetnosy: + scoder
messages: + msg246611
2015-07-11 16:23:08steven.dapranosetnosy: + steven.daprano
messages: + msg246608
2015-07-11 16:18:41serhiy.storchakasetnosy: + benjamin.peterson
messages: + msg246607
2015-07-11 15:23:53candidecreate