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

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

Closed
15r10nk opened this issue Sep 7, 2023 · 3 comments
Closed

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

15r10nk opened this issue Sep 7, 2023 · 3 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@15r10nk
Copy link
Contributor

15r10nk commented Sep 7, 2023

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

@15r10nk 15r10nk added the type-bug An unexpected behavior, bug, or error label Sep 7, 2023
@AlexWaygood
Copy link
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

@JelleZijlstra
Copy link
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.

@pablogsal
Copy link
Member

pablogsal commented Sep 8, 2023

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

pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
… to avoid false positives

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
… to avoid false positives

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
… to avoid false positives

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
pablogsal added a commit that referenced this issue Sep 8, 2023
pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
…strings to avoid false positives (pythonGH-109121).

(cherry picked from commit 5bda2f6)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
pablogsal added a commit to pablogsal/cpython that referenced this issue Sep 8, 2023
…strings to avoid false positives (pythonGH-109121).

(cherry picked from commit 5bda2f6)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Yhg1s pushed a commit that referenced this issue Sep 12, 2023
…s to avoid false positives (GH-109121). (#109155)

(cherry picked from commit 5bda2f6)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants