Add support for PEP 750 template strings#440
Conversation
|
Looks like most of CI is passing, bar |
| def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]: | ||
| yield "t'" | ||
| yield from _join(self.values, "", flat=flat) | ||
| yield "'" |
There was a problem hiding this comment.
Are we sure that using hardcoded single (or double) quotes will work in every case? 🤔
There was a problem hiding this comment.
I referred to the implementation of ExprJoinedStr for this. As far as I can tell, this iterate method is only used when round-tripping in tests or dumping expressions by __str__ - it doesn't seem like it's very critical? Please correct me if I misunderstand.
I imagine this round-tripping could break when strings contain certain types of quotes - for example, if input value is t"a'b", converting this to t'a'b' would invalidate syntax. However, if we have the same problem with ExprJoinedStr and ExprTemplateStr, I propose creating a new project issue to improve quote handling.
There was a problem hiding this comment.
It's critical: it must yield correct code. This code is then parsed to be highlighted, formatted etc. by Black/Ruff/Pygments/else in downstream tools like mkdocstrings-python.
However, if we have the same problem with ExprJoinedStr and ExprTemplateStr, I propose creating a new project issue to improve quote handling.
You make a good point. Lets handle that in a new issue then 🙂
Oh we probably just need to add the two new classes |
Thanks for the review! Looks like most of CI is passing now, bar the 3.15 tests bombing out. Let me know how you'd like to move forward ^^ |
|
Yep, failures on 3.15 are fine, I'll investigate them later 🙂 Let me know when you think this is ready to merge! |
d4261d3 to
06c863f
Compare
|
Thanks again! |
For reviewers
Description of the change
Add expressions for
ast.TemplateStrandast.Interpolation, which were added in Python 3.14. Round-tripping (e.g.tests/test_expressions.py::test_expressions) does not work with theconversionorformat_specfields, though this is consistent with the current implementation ofExprJoinedStrandExprFormatted.Relevant resources