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

Improve syntax error for invalid comparisons #87963

Closed
pablogsal opened this issue Apr 9, 2021 · 8 comments
Closed

Improve syntax error for invalid comparisons #87963

pablogsal opened this issue Apr 9, 2021 · 8 comments

Comments

@pablogsal
Copy link
Member

@pablogsal pablogsal commented Apr 9, 2021

BPO 43797
Nosy @aroberge, @serhiy-storchaka, @lysnikolaou, @pablogsal
PRs
  • #25317
  • #25375
  • #25390
  • 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 = <Date 2021-04-12.15:59:58.988>
    created_at = <Date 2021-04-09.19:11:31.900>
    labels = []
    title = 'Improve syntax error for invalid comparisons'
    updated_at = <Date 2021-04-13.16:51:41.828>
    user = 'https://github.com/pablogsal'

    bugs.python.org fields:

    activity = <Date 2021-04-13.16:51:41.828>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-12.15:59:58.988>
    closer = 'pablogsal'
    components = []
    creation = <Date 2021-04-09.19:11:31.900>
    creator = 'pablogsal'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43797
    keywords = ['patch']
    message_count = 8.0
    messages = ['390659', '390700', '390703', '390728', '390729', '390730', '390862', '390977']
    nosy_count = 4.0
    nosy_names = ['aroberge', 'serhiy.storchaka', 'lys.nikolaou', 'pablogsal']
    pr_nums = ['25317', '25375', '25390']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue43797'
    versions = []

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 9, 2021

    Improve syntax error for invalid comparisons such as:

    >>> if x = 3:
      File "<stdin>", line 1
        if x = 3:
             ^
    SyntaxError: invalid syntax

    to print:

    >>> if x = 3:
      File "<stdin>", line 1
        if x = 3:
             ^
    SyntaxError: cannot assign to name. Maybe you meant '==' or ':=' instead of '='?

    instead

    @serhiy-storchaka
    Copy link
    Member

    @serhiy-storchaka serhiy-storchaka commented Apr 10, 2021

    "cannot assign to name" looks wrong. Usually we can assign to name, unless it is a keyword. And the problem is not that we cannot assign to name (we can), but that is is an invalid syntax, assignment is a statement, not an expression.

    Seems it handles only simplest cases with "=" in "if". It does not handle "while x = 3" and "if y and x = 3". Could it be possible to make it more general.

    @serhiy-storchaka
    Copy link
    Member

    @serhiy-storchaka serhiy-storchaka commented Apr 10, 2021

    I tried to add

    | a=NAME '=' {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
            a, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
    | a=bitwise_or '=' {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
            a, "invalid syntax. Maybe you meant '==' instead of '='?") }
    

    in invalid_named_expression, but it does not have any effect.

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 10, 2021

    These are good points, let me investigate a bit

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 10, 2021

    in invalid_named_expression, but it does not have any effect.

    That is because the rule for named_expressions are:

    named_expression[expr_ty]:
    | a=NAME ':=' ~ b=expression
    | expression !':='
    | invalid_named_expression

    and the second alternative (| expression !':=') succeeds with an assignment, not letting the invalid_named_expression run

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 10, 2021

    I have updated the PR with:

    • Works for while statements (we can make it work for other kinds of statements but is a bit tricky because those have different grammar paths. We could do this in other PRs.

    • I changed the error message for when the LHS is a name. In other cases we still want the special error message. For example:

    >>> f() = 1
      File "<stdin>", line 1
        f() = 1
            ^
    SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='?

    If we still want something different we could say:

    "cannot assign to ... here". The "here" makes it clear that in other places could be possible to do an assignment (for instance with names or attributes).

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 12, 2021

    New changeset b86ed8e by Pablo Galindo in branch 'master':
    bpo-43797: Improve syntax error for invalid comparisons (bpo-25317)
    b86ed8e

    @pablogsal
    Copy link
    Member Author

    @pablogsal pablogsal commented Apr 13, 2021

    New changeset 30ed93b by Pablo Galindo in branch 'master':
    bpo-43797: Handle correctly invalid assignments inside function calls and generators (GH-25390)
    30ed93b

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants