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

Invalid syntax error in multi-line f-string with format spec #6077

Closed
dhruvmanila opened this issue Oct 3, 2023 · 7 comments
Closed

Invalid syntax error in multi-line f-string with format spec #6077

dhruvmanila opened this issue Oct 3, 2023 · 7 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@dhruvmanila
Copy link

Describe the bug

Basically the same bug as reported in CPython (python/cpython#110259) exists in Pyright's lexer as well.

Code or Screenshots

f"""__{
	a:d
}__"""

Running pyright on the above code gives:

$ pyright ~/playground/ruff/fstring.py
/Users/dhruv/playground/ruff/fstring.py
  /Users/dhruv/playground/ruff/fstring.py:3:2 - error: String literal is unterminated
  /Users/dhruv/playground/ruff/fstring.py:2:5 - error: "x" is not defined (reportUndefinedVariable)
2 errors, 0 warnings, 0 informations 

VS Code extension or command-line

Running through the command-line using 1.1.329

@dhruvmanila dhruvmanila added the bug Something isn't working label Oct 3, 2023
@erictraut
Copy link
Collaborator

I'm not able to repro the error with the code you've supplied above. Could you verify that your code sample is correct? For me, pyright emits another (correct) error indicating that a is an undefined symbol. This is in accordance with the python interpreter, which emits the same error. If I modify the code to remove that error and replace a:d with a valid expression, pyright emits no errors, nor does the Python interpreter.

a = 1
d = 1

f"""__{
	a + d
}__"""

@erictraut erictraut added the question Further information is requested label Oct 3, 2023
@dhruvmanila
Copy link
Author

Oh sorry, there are two cases here. The error thrown would be when using single quotes:

f"__{
	a:d
}__"

The problem that exists when using triple-quotes is that the format spec includes the newline character which is invalid. I'm not sure if that's relevant in Pyright.

@erictraut
Copy link
Collaborator

Ah, I think you meant to use single quotes rather than triple quotes.

f"__{
    x:d
}__"

Pyright's behavior matches that of the Python interpreter, so I consider it correct. I used the CPython test cases for PEP 701 to validate that pyright was working correctly here.

If the CPython devs ultimately determine that this is a bug in the Python interpreter, then I'll make the corresponding change in pyright. But for now, I consider this to be correct behavior.

@erictraut erictraut added as designed Not a bug, working as intended and removed question Further information is requested labels Oct 3, 2023
@erictraut erictraut closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2023
@erictraut
Copy link
Collaborator

erictraut commented Oct 3, 2023

Oh, I just noticed that @pablogsal has already acknowledged that this is a bug in CPython and has submitted a PR to address it.

@erictraut erictraut reopened this Oct 3, 2023
@erictraut erictraut removed the as designed Not a bug, working as intended label Oct 3, 2023
erictraut pushed a commit that referenced this issue Oct 4, 2023
…ote form of a format string that uses a format specifier expression. This is also a bug in the Python 3.12 interpreter that is being fixed. This addresses #6077.
erictraut added a commit that referenced this issue Oct 4, 2023
…ote form of a format string that uses a format specifier expression. This is also a bug in the Python 3.12 interpreter that is being fixed. This addresses #6077. (#6082)

Co-authored-by: Eric Traut <erictr@microsoft.com>
@erictraut
Copy link
Collaborator

This issue will be addressed in the next release of pyright.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Oct 4, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.330, which I just published. It will also be included in an upcoming release of pylance.

@pablogsal
Copy link

Friendly reminder to review the discussion in python/cpython#110259 so you end implementing the same as CPython 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants