Skip to content

Formatter can reuse nested quotes before 3.12 #20774

@ntBre

Description

@ntBre

Summary

While updating our black compatibility tests for #20768, I ran into a failure related to this test case:

f"{f'''{'nested'} inner'''} outer"

This is valid on Python versions before 3.12 (when quote reuse became allowed in f-strings), but the formatted result converts the innermost quotes to double quotes, making it invalid:

f"{f'''{"nested"} inner'''} outer"

I was hoping to put up a quick PR fixing this, but I'm still working on it and figured I might as well open an issue. I bisected the issue to #13906, but that's not too helpful because this PR is when we first introduced f-string formatting, as Micha pointed out on Discord.

Something interesting I've found besides the bug itself is that our main format test doesn't really catch this. fstring.py actually has more unsupported syntax errors before formatting than after, which I think prevents our error fingerprinting from working as intended. Our test runner produces a snapshot with the invalid quotes above, without panicking from the new unsupported syntax error, like the black test did.

I've played a bit with more information we could add to the fingerprint, but unfortunately I haven't found a good solution there yet either.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions