fix: Render f-strings and t-strings with correct quote delimiters#455
Open
johnslavik wants to merge 5 commits into
Open
fix: Render f-strings and t-strings with correct quote delimiters#455johnslavik wants to merge 5 commits into
johnslavik wants to merge 5 commits into
Conversation
ExprJoinedStr.iterate() and ExprTemplateStr.iterate() previously
hardcoded a single-quote delimiter, producing invalid Python source
when the literal parts of the f-string contained apostrophes.
Port ast.unparse's visit_JoinedStr quote-selection algorithm: pre-render
all parts (literals get brace-doubled and control-char-escaped, expression
parts get flattened to string), then pick the first quote type from
("'", '"', "'''", '"""') that doesn't conflict with any part.
Also guard _build_constant's parse_strings path with `not in_formatted_str`
so string literals inside f-string expressions ({...}) are never
mistakenly compiled as type annotations.
…) result The compile() call with ast.PyCF_ONLY_AST returns ast.AST but ty cannot infer the narrower ast.Module type. Existing tests in the same file already use the same ty:ignore directive; apply it consistently to the new tests. 💞 Generated with the [darling work system](https://github.com/johnslavik/darling)
Replace manual join-via-.name with str(value) — Expr.__str__ already does the same thing, so this is a straight equivalence. 💞 Generated with the [darling work system](https://github.com/johnslavik/darling)
416b258 to
419c8e5
Compare
The block of t-string quote-delimiter test cases should not be removed when Python 3.13 reaches EOL — t-strings become universally available then and the version guard can simply be dropped. Replace "Remove block" with "Replace block with lines 3-5" so YORE strips the `sys.version_info >= (3, 14)` wrapper and keeps the three test strings. 💞 Generated with the [darling work system](https://github.com/johnslavik/darling)
5c609ff to
238453a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #444
Still in progress (I'm validating the implementation), but that's more or less what I'd like to land.
ExprJoinedStr.iterate()andExprTemplateStr.iterate()previously hardcoded a single-quote delimiter, producing invalid Python source when the literal parts of the f-string contained apostrophes.Port ast.unparse's visit_JoinedStr quote-selection algorithm: pre-render all parts (literals get brace-doubled and control-char-escaped, expression parts get flattened to string), then pick the first quote type from ("'", '"', "'''", '"""') that doesn't conflict with any part.
Also guard _build_constant's parse_strings path with
not in_formatted_strso string literals inside f-string expressions ({...}) are never mistakenly compiled as type annotations.For reviewers
Description of the change