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 errors messages in f-string syntax errors #91285

Closed
macgors mannequin opened this issue Mar 26, 2022 · 9 comments
Closed

Improve errors messages in f-string syntax errors #91285

macgors mannequin opened this issue Mar 26, 2022 · 9 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@macgors
Copy link
Mannequin

macgors mannequin commented Mar 26, 2022

BPO 47129
Nosy @ericvsmith, @lysnikolaou, @pablogsal, @macgors
PRs
  • bpo-47129: Add more informative messages to f-string syntax errors #32127
  • 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 = 'https://github.com/ericvsmith'
    closed_at = <Date 2022-03-28.21:09:44.786>
    created_at = <Date 2022-03-26.15:08:27.680>
    labels = ['interpreter-core', 'type-bug']
    title = 'Improve errors messages in f-string syntax errors'
    updated_at = <Date 2022-03-28.21:12:40.376>
    user = 'https://github.com/macgors'

    bugs.python.org fields:

    activity = <Date 2022-03-28.21:12:40.376>
    actor = 'macgors'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2022-03-28.21:09:44.786>
    closer = 'eric.smith'
    components = ['Parser']
    creation = <Date 2022-03-26.15:08:27.680>
    creator = 'macgors'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 47129
    keywords = ['patch']
    message_count = 9.0
    messages = ['416069', '416075', '416077', '416095', '416127', '416128', '416215', '416216', '416217']
    nosy_count = 4.0
    nosy_names = ['eric.smith', 'lys.nikolaou', 'pablogsal', 'macgors']
    pr_nums = ['32127']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue47129'
    versions = []

    @macgors
    Copy link
    Mannequin Author

    macgors mannequin commented Mar 26, 2022

    When an empty expression is provided in curly brackets inside an f-string we get Syntax Error: "f-string: empty expression not allowed".

    This is correct, however error with the same message is raised in the following cases: f"{!foo}", f"{!}", f"{:bar}", f"{=}", etc.
    For example, should someone make an error and try to negate an expression with '!' instead of 'not' (i.e. f"{!True}"), they would get a confusing error, saying that the expression is empty, when it isn't.

    I propose we raise an error with the message: "f-string: optional specifier '%c' must follow a non-empty expression.

    @macgors macgors mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 26, 2022
    @ericvsmith
    Copy link
    Member

    I'm not convinced this is an improvement. I don't think someone trying '!' in an f-string expression happens often enough to worry about that specific error message.

    And a generic "optional specifier" isn't great. If we're going to do this, it should reflect the actual thing that's present: a conversion specifier, a format specifier, or debug specifier. And "optional" doesn't add anything, since it's actually present in this string.

    I'll give it some more thought. I can't come up with an error message I actually like. The focus still needs to be on the empty expression.

    @macgors
    Copy link
    Mannequin Author

    macgors mannequin commented Mar 26, 2022

    Understandable. In this case I can maybe propose something along the lines of "f-string: empty expression not allowed (Conversion specifier '!' can only follow non-empty expressions)", and so on for each of the specifiers.

    This way the original message is still present and we just add some more information. I'm open to any of your ideas.

    @ericvsmith
    Copy link
    Member

    From a suggestion by @jelle on the python discord server: how about just "f-string: expression required before '!'"?

    @lysnikolaou
    Copy link
    Contributor

    I like the newest suggestion by @jelle better as well.

    @macgors
    Copy link
    Mannequin Author

    macgors mannequin commented Mar 27, 2022

    I agree as well. I will change it to the new version :) Thanks for your input in this.

    @ericvsmith
    Copy link
    Member

    New changeset 7b44ade by Maciej Górski in branch 'main':
    bpo-47129: Add more informative messages to f-string syntax errors (32127)
    7b44ade

    @ericvsmith
    Copy link
    Member

    Thanks, @macgors!

    @ericvsmith ericvsmith added the type-bug An unexpected behavior, bug, or error label Mar 28, 2022
    @macgors
    Copy link
    Mannequin Author

    macgors mannequin commented Mar 28, 2022

    Thanks for helping me with my very first python contribution! @eric.smith @lys.nikolaou

    @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
    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

    2 participants