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

Keyword can't be an expression? #75041

Closed
vedgar mannequin opened this issue Jul 5, 2017 · 13 comments
Closed

Keyword can't be an expression? #75041

vedgar mannequin opened this issue Jul 5, 2017 · 13 comments

Comments

@vedgar
Copy link
Mannequin

vedgar mannequin commented Jul 5, 2017

BPO 30858
Nosy @gvanrossum, @rhettinger, @ncoghlan, @bitdancer, @serhiy-storchaka, @vedgar, @lysnikolaou, @pablogsal, @iritkatriel
PRs
  • bpo-30858: Improve error location for expressions with assignments #23753
  • 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 2020-12-13.16:47:04.074>
    created_at = <Date 2017-07-05.17:21:20.397>
    labels = []
    title = "Keyword can't be an expression?"
    updated_at = <Date 2020-12-13.16:47:04.073>
    user = 'https://github.com/vedgar'

    bugs.python.org fields:

    activity = <Date 2020-12-13.16:47:04.073>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-13.16:47:04.074>
    closer = 'pablogsal'
    components = []
    creation = <Date 2017-07-05.17:21:20.397>
    creator = 'veky'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30858
    keywords = ['patch']
    message_count = 13.0
    messages = ['297772', '297798', '297800', '297805', '297828', '297899', '324138', '324139', '324168', '382547', '382918', '382933', '382935']
    nosy_count = 9.0
    nosy_names = ['gvanrossum', 'rhettinger', 'ncoghlan', 'r.david.murray', 'serhiy.storchaka', 'veky', 'lys.nikolaou', 'pablogsal', 'iritkatriel']
    pr_nums = ['23753']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue30858'
    versions = []

    @vedgar
    Copy link
    Mannequin Author

    vedgar mannequin commented Jul 5, 2017

    Look at this (from https://www.quora.com/Is-end1-a-keyword-in-Python-3-6-1):

    print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
             ^
    SyntaxError: keyword can't be an expression

    Wouldn't it be better if the message said "keyword for an argument must be a simple name"? Or something like that. Newbies, when they think of keywords, they think something from keyword.kwlist, not something used to pass arguments to a function in a particular way.

    @bitdancer
    Copy link
    Member

    I think the current error message is more informative than your suggestion, myself. However, the message could say "keyword argument name" instead of just "keyword", which I think would be quite a bit clearer.

    I seem to remember another discussion where I suggested disambiguating "keyword" by using "keyword argument name", but I have no idea how to phrase a search to find that issue :(

    @serhiy-storchaka
    Copy link
    Member

    bpo-29951.

    @vedgar
    Copy link
    Mannequin Author

    vedgar mannequin commented Jul 6, 2017

    I agree it's more _correct_. But it's also more confusing. As far as I can tell, when writing error messages, we tend to minimize confusion, not maximize correctness.

    Of course, it would be great to have both. Your "keyword argument name" seems pretty good.

    @bitdancer
    Copy link
    Member

    If I saw your message, I would think "what is a 'simple name'?". There's no glossary entry for that, nor is it a concept used elsewhere in the documentation as far as I remember. One could instead use "single identifier", but the problem with both of those is that "end" *is* a simple name/single identifier. The error isn't that an identifier was not used, the error is that an expression was used. We unfortunately can't read the mind of the programmer to know that the *actual* error was using a '+' instead of a ','.

    @rhettinger
    Copy link
    Contributor

    FWIW, I prefer the current error message and my students don't seem to have issues with it.

    No matter what wording we put in, someone will always find a way to misread it, in part because the "end6 + end=' '" example isn't a simple mistake, it reflects an incorrect mental model that isn't easily fixed by an error message.

    @ncoghlan
    Copy link
    Contributor

    I just ran into this, and found the existing error message *incredibly* confusing. My immediate reaction was "There's no keyword in that line, what are you complaining about?".

    An error message that said "Keyword argument name must be an identifier" would have been *far* more useful, and far less confusing.

    @ncoghlan
    Copy link
    Contributor

    The current error message is also outright incorrect, since simple names *are* valid expressions - the actual problem being reported is that binary expressions (veky's case) and strings (my case) *aren't* identifiers.

    @vedgar
    Copy link
    Mannequin Author

    vedgar mannequin commented Aug 27, 2018

    Nick, thanks for validating my pain. :-|

    In the meantime, I found out Python already knows more finely what kind of expression something is, and uses that knowledge in the error messages. Look:

        >>> a + b = 4
        SyntaxError: can't assign to operator
        >>> a(b) = 4
        SyntaxError: can't assign to function call
        >>> 'a' = 4
        SyntaxError: can't assign to literal

    So, how about "keyword argument name cannot be an operator / function call / literal"? (Of course, if you ask me, "operator" is also unfortunate, but at least there is a precedent.)

    @iritkatriel
    Copy link
    Member

    I get this now on 3.10:

    >>> print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
      File "<stdin>", line 1
        print(end1 + end2 + end3 + end4 + end5 + end6 + end=' ')
              ^
    SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

    @lysnikolaou
    Copy link
    Contributor

    I like the current error message. What bugs me a little is the position of the caret, which I think is a bit misleading.

    Guido, Pablo, should we move the caret to point at the = sign or whatever comes after it? I think I prefer the former.

    @pablogsal
    Copy link
    Member

    +1 for the = sign

    @pablogsal
    Copy link
    Member

    New changeset 43c4fb6 by Pablo Galindo in branch 'master':
    bpo-30858: Improve error location for expressions with assignments (GH-23753)
    43c4fb6

    @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

    7 participants