Skip to content

misleading SyntaxError (f-string and type alias related) #109114

Closed

Description

@15r10nk

Bug report

Bug description:

The following code produces a misleading syntax error.

lambda name_3=f'{name_4}': {name_3}

type {}[name_4] = {}

output (Python 3.12.0rc2+):

  File "/home/frank/projects/pysource-codegen/bug.py", line 1
    lambda name_3=f'{name_4}': {name_3}
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: f-string: lambda expressions are not allowed without parentheses

The first line should be valid python syntax and I would expect to get an syntax error for the wrong type alias name.

side note:
This is generated code. I found this issue during my work for the python 3.12 support for pysource-codegen.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 7, 2023
  2. AlexWaygood commented on Sep 7, 2023

    @AlexWaygood
    Member

    The first line should be valid python syntax

    Cc. @pablogsal

    and I would expect to get an syntax error for the wrong type alias name.

    Cc. @JelleZijlstra

  3. JelleZijlstra commented on Sep 7, 2023

    @JelleZijlstra
    Member

    The wrong type alias does produce a syntax error when you put it by itself:

    >>> type {}[name_4] = {}
      File "<stdin>", line 1
        type {}[name_4] = {}
             ^
    SyntaxError: invalid syntax
    

    I suspect there's some wrong heuristic in the parser where it simply shows the SyntaxError in the wrong place. That's outside my area of expertise though, so I'll leave this to Pablo.

  4. pablogsal commented on Sep 8, 2023

    @pablogsal
    Member

    The problem is that the parser finds the issue in the error pass with the lambda first, even if is correct: that's why the type alias by itself raises. You can technically put whatever you want there that's invalid

  5. added 6 commits that reference this issue on Sep 8, 2023
  6. added a commit that references this issue on Sep 8, 2023
  7. added a commit that references this issue on Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions