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

Insufficient error message with incorrect formated string literal #75323

Closed
chris259263 mannequin opened this issue Aug 8, 2017 · 9 comments
Closed

Insufficient error message with incorrect formated string literal #75323

chris259263 mannequin opened this issue Aug 8, 2017 · 9 comments
Labels
3.7 (EOL) end of life 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@chris259263
Copy link
Mannequin

chris259263 mannequin commented Aug 8, 2017

BPO 31140
Nosy @Cito, @ericvsmith
PRs
  • bpo-30465: Fix lineno and col_offset in fstring AST nodes #1800
  • 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 2022-03-29.00:11:29.166>
    created_at = <Date 2017-08-08.08:55:58.045>
    labels = ['interpreter-core', 'type-bug', '3.7', '3.9']
    title = 'Insufficient error message with incorrect formated string literal'
    updated_at = <Date 2022-03-29.00:56:04.675>
    user = 'https://bugs.python.org/chris259263'

    bugs.python.org fields:

    activity = <Date 2022-03-29.00:56:04.675>
    actor = 'PeterL777'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-29.00:11:29.166>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2017-08-08.08:55:58.045>
    creator = 'chris.259263'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31140
    keywords = []
    message_count = 9.0
    messages = ['299901', '299903', '299906', '299907', '309773', '358410', '416234', '416236', '416237']
    nosy_count = 5.0
    nosy_names = ['cito', 'eric.smith', 'chris.259263', 'philiprowlands', 'PeterL777']
    pr_nums = ['1800']
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31140'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.9']

    @chris259263
    Copy link
    Mannequin Author

    chris259263 mannequin commented Aug 8, 2017

    Trying to run a script with a bug in a formated string literal (in the example the ":" is missing) throws an "invalid syntax" error at the first expression of the script.

    Example:

    import math
    a = "a"
    b = f"{a>2s}"   # forgotten ":", should be f"{a:>2s}"

    Running this script throws an "invalid syntax" error in the first line "import math".

    In a large program this behavior makes it nearly impossible to find the real bug in the program as the error message does not give any hint about the line or the type of the error in the program.

    Using Python 3.6.2, IDLE 3.6.2 on Mac macOS Sierra (10.12.1)

    @chris259263 chris259263 mannequin added the type-bug An unexpected behavior, bug, or error label Aug 8, 2017
    @ericvsmith
    Copy link
    Member

    On 3.6.1 on Windows, I get:

    % python3 bpo-31140.py
    File "<fstring>", line 1
    (a>2s)
    ^
    SyntaxError: invalid syntax

    Which is far from ideal, but at least points to (a portion of) the correct text.

    I won't have access to a copy of 3.6.2 or 3.7 until later today. Can you show the exact output from 3.6.2?

    @ericvsmith ericvsmith added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Aug 8, 2017
    @chris259263
    Copy link
    Mannequin Author

    chris259263 mannequin commented Aug 8, 2017

    Hi Eric,

    running the script from the terminal on my system (macOS, Python 3.6.2) gives the same ErrorMessage as you stated.

    The problem is the output "line 1".

    Running the script from IDLE (3.6.2), the error points to the first line. And in IDLE you do not get the useful hint "(a>2s)" but just "SyntaxError: invalid syntax".

    @ericvsmith
    Copy link
    Member

    Thanks. There was some work on this recently: I'll try and check 3.7 later today.

    @ericvsmith ericvsmith added the 3.7 (EOL) end of life label Sep 4, 2017
    @Cito
    Copy link
    Mannequin

    Cito mannequin commented Jan 10, 2018

    I can confirm that the problem still exists in Python 3.6.4 and 3.7.0a4.

    Here is another way to demonstrate it:

    Create the following file test.py:

        # test
        hello = f"{world)}"

    Note that there is a syntax error in the f-string in line 2 which has a closing parentheses, but no opening one.

    Import this from Python 3.6.4 or 3.7.0a4:

    >>> import test
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<fstring>", line 1
        (world))
               ^
    SyntaxError: unexpected EOF while parsing

    The problem here is that the error message does not contain the name of the erroneous file (test.py), points to a wrong line (line 1 instead of line 2), and also shows parentheses instead of braces around the word "world", which are not there in the original code. This can make it hard to locate such errors.

    Note that when there are other kinds of errors in the f-string, or other kinds of "unexpected EOF" in the imported file, the errorenous file is usually reported correctly in the error message. Only certain kinds of syntax errors in f-strings seem to be problematic.

    @philiprowlands
    Copy link
    Mannequin

    philiprowlands mannequin commented Dec 15, 2019

    Status as of 3.9.0a1:

    ======
    test.py above appears fixed, i.e. reasonable error message.

    $ ./python test.py
      File "/home/bob/pybug/Python-3.9.0a1/test.py", line 2
        hello = f"{world)}"
                ^
    SyntaxError: f-string: unmatched ')'

    ======
    bpo-31140.py is not as bad a initially reported, but still gives the wrong line number.

    $ ./python bpo-31140.py
      File "<fstring>", line 1
        (a>2s)
            ^
    SyntaxError: invalid syntax

    =======
    And my own example which led me to this bug. The syntax error is on line 3, not line 1.

    $ cat fruit.py
    pass
    pass
    s = f"{My favourite fruit is {apple}}"
    
    $ ./python -V
    Python 3.9.0a1
    
    $ ./python fruit.py
      File "<fstring>", line 1
        (My favourite fruit is {apple})
            ^
    SyntaxError: invalid syntax

    @philiprowlands philiprowlands mannequin added the 3.9 only security fixes label Dec 15, 2019
    @PeterL777
    Copy link
    Mannequin

    PeterL777 mannequin commented Mar 29, 2022

    I'm not getting the problem on 3.9.7 on Windows.
    Did get it on 3.7 (3.7.11?) on a different Windows machine last week.
    Not getting the problem on 3.10.4

    The wrong line number is a problem for IDLE's syntax highlighter, that marks the first line as a Syntax Error, even if that line is a comment.

    This is an issue for ArcGIS Pro users, which is (I think) currently shipping with Python 3.7.11, so those users may come across it. Best solution would be for Esri to update their Python version to something newer.
    https://support.esri.com/en/technical-article/000013224

    @ericvsmith
    Copy link
    Member

    Yes, they should update Python. A lot of work went in to fixing these issues, and won't be backported.

    @PeterL777
    Copy link
    Mannequin

    PeterL777 mannequin commented Mar 29, 2022

    Thanks Eric.

    @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
    3.7 (EOL) end of life 3.9 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

    1 participant