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

Update FormatStringContinuation to use the new f-string tokens #7517

Closed
Tracked by #6502
dhruvmanila opened this issue Sep 19, 2023 · 5 comments · Fixed by #7586 or #7597
Closed
Tracked by #6502

Update FormatStringContinuation to use the new f-string tokens #7517

dhruvmanila opened this issue Sep 19, 2023 · 5 comments · Fixed by #7586 or #7597
Assignees
Labels
formatter Related to the formatter python312 Related to Python 3.12

Comments

@dhruvmanila
Copy link
Member

The FormatStringContinuation implementation uses the full lexer for comment handling. It expects only a limited set of tokens (String, Newline, NonLogicalNewline, Comment, Indent, Dedent) which is expected given that the lexing is performed only on the string literals.

Now, with PEP 701, the f-string literal will instead emit FStringStart, FStringMiddle, and FStringEnd. Not only that, it'll also emit other tokens for the expression part of the f-string. This poses a challenge as currently it panics for any unexpected token encountered:

token => unreachable!("Unexpected token {token:?}"),

@dhruvmanila dhruvmanila added formatter Related to the formatter python312 Related to Python 3.12 labels Sep 19, 2023
@dhruvmanila dhruvmanila added this to the Formatter: Beta milestone Sep 19, 2023
@MichaReiser
Copy link
Member

Is this blocking for landing PEP701?

@dhruvmanila
Copy link
Member Author

It's one of the failing test in the CI: https://github.com/astral-sh/ruff/actions/runs/6236001637/job/16926357093?pr=7376#step:7:2341

It shouldn't block Ruff in general but the formatter will panic when a f-string is encountered in implicit string concatenation with comments in it. For example,

foo = (
    f"bar"
    # comment
    "foo"
)

@MichaReiser
Copy link
Member

Yeah, I don't think we can break the formatter so significantly at this stage

@dhruvmanila dhruvmanila self-assigned this Sep 22, 2023
@dhruvmanila
Copy link
Member Author

I've started looking into it. A basic implementation of using the outermost f-string range and ignoring all tokens within a f-string works but I'll look at if there are any other cases which may break this.

@dhruvmanila
Copy link
Member Author

Currently, this is completed in a way so as to not block merging PEP 701. Tracking issue for handling comments and quotes in f-string is at #7594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter python312 Related to Python 3.12
Projects
None yet
2 participants