Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not operator expression raising a syntax error #68800

Closed
candide mannequin opened this issue Jul 11, 2015 · 11 comments
Closed

not operator expression raising a syntax error #68800

candide mannequin opened this issue Jul 11, 2015 · 11 comments
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@candide
Copy link
Mannequin

candide mannequin commented Jul 11, 2015

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']

    @candide
    Copy link
    Mannequin Author

    candide mannequin commented Jul 11, 2015

    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.

    @candide candide mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jul 11, 2015
    @serhiy-storchaka
    Copy link
    Member

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

    @stevendaprano
    Copy link
    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

    @scoder
    Copy link
    Contributor

    scoder commented Jul 11, 2015

    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.

    @mrabarnett
    Copy link
    Mannequin

    mrabarnett mannequin commented Jul 11, 2015

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

    @scoder
    Copy link
    Contributor

    scoder commented Jul 11, 2015

    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.

    @vadmium
    Copy link
    Member

    vadmium commented Jul 12, 2015

    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.

    @vadmium
    Copy link
    Member

    vadmium commented Jul 12, 2015

    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

    @iritkatriel
    Copy link
    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

    @iritkatriel iritkatriel added the 3.11 only security fixes label Sep 4, 2021
    @pablogsal
    Copy link
    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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @encukou
    Copy link
    Member

    encukou commented Mar 26, 2024

    Fixed in #28170.
    Thanks to everyone involved!

    @encukou encukou closed this as completed Mar 26, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    7 participants