Skip to content

not operator expression raising a syntax error #68800

Closed

Description

@candide
mannequin
BPO 24612
Nosy @scoder, @benjaminp, @aroberge, @ezio-melotti, @stevendaprano, @vadmium, @serhiy-storchaka, @pablogsal, @iritkatriel
PRs
  • bpo-24612: Improve syntax error for 'not' after an operator #28170
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2015-07-11.15:23:53.358>
    labels = ['interpreter-core', 'type-bug', '3.11']
    title = 'not operator expression raising a syntax error'
    updated_at = <Date 2021-09-05.01:21:27.256>
    user = 'https://bugs.python.org/candide'

    bugs.python.org fields:

    activity = <Date 2021-09-05.01:21:27.256>
    actor = 'aroberge'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2015-07-11.15:23:53.358>
    creator = 'candide'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 24612
    keywords = ['patch']
    message_count = 10.0
    messages = ['246606', '246607', '246608', '246611', '246612', '246613', '246619', '246620', '401055', '401063']
    nosy_count = 11.0
    nosy_names = ['scoder', 'benjamin.peterson', 'aroberge', 'ezio.melotti', 'mrabarnett', 'steven.daprano', 'martin.panter', 'serhiy.storchaka', 'candide', 'pablogsal', 'iritkatriel']
    pr_nums = ['28170']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue24612'
    versions = ['Python 3.11']

    Activity

    1. candide commented on Jul 11, 2015

      candidemannequin
      MannequinAuthor

      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.

    2. added
      interpreter-core
      type-bug
      on Jul 11, 2015
    3. serhiy-storchaka commented on Jul 11, 2015

      @serhiy-storchaka
      Member

      "not not 0" is compiled successful, while "+ not 0", "- not 0", and "~ not 0" are rejected.

    4. stevendaprano commented on Jul 11, 2015

      @stevendaprano
      Member
      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
    5. scoder commented on Jul 11, 2015

      @scoder
      Contributor

      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.

    6. mrabarnett commented on Jul 11, 2015

      mrabarnettmannequin
      Mannequin

      "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.

    7. scoder commented on Jul 11, 2015

      @scoder
      Contributor

      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.

    8. vadmium commented on Jul 12, 2015

      @vadmium
      Member

      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.

    9. vadmium commented on Jul 12, 2015

      @vadmium
      Member

      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
    10. iritkatriel commented on Sep 4, 2021

      @iritkatriel
      Member

      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
    11. added
      3.11
      on Sep 4, 2021
    12. pablogsal commented on Sep 4, 2021

      @pablogsal
      Member

      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

    13. transferred this issue fromon Apr 10, 2022
    14. encukou commented on Mar 26, 2024

      @encukou
      Member

      Fixed in #28170.
      Thanks to everyone involved!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions