Skip to content

Conversation

@1borgy
Copy link

@1borgy 1borgy commented Dec 5, 2025

For reviewers

  • I did not use AI
  • I used AI and thoroughly reviewed every code/docs change

Description of the change

Add expressions for ast.TemplateStr and ast.Interpolation, which were added in Python 3.14. Round-tripping (e.g. tests/test_expressions.py::test_expressions) does not work with the conversion or format_spec fields, though this is consistent with the current implementation of ExprJoinedStr and ExprFormatted.

Relevant resources

@1borgy
Copy link
Author

1borgy commented Dec 5, 2025

Looks like most of CI is passing, bar config/test_api.py::test_api_matches_inventory for python<3.14. The suggestion of make run mkdocs build did not generate any changes to site/objects.inv for me. For the sake of not overusing GHA runner resources, I'll wait for a maintainer's blessing before investigating further.

Copy link
Member

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @1borgy, super clean! Just a few comments.

Comment on lines +940 to +943
def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]:
yield "t'"
yield from _join(self.values, "", flat=flat)
yield "'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that using hardcoded single (or double) quotes will work in every case? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@pawamoy
Copy link
Member

pawamoy commented Jan 13, 2026

Looks like most of CI is passing, bar config/test_api.py::test_api_matches_inventory for python<3.14. The suggestion of make run mkdocs build did not generate any changes to site/objects.inv for me.

Oh we probably just need to add the two new classes ExprInterpolation and ExprTemplateStr to docs/reference/api/expressions.md.

@1borgy
Copy link
Author

1borgy commented Jan 14, 2026

Thanks @1borgy, super clean! Just a few comments.

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 ^^

@pawamoy
Copy link
Member

pawamoy commented Jan 14, 2026

Yep, failures on 3.15 are fine, I'll investigate them later 🙂 Let me know when you think this is ready to merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants