Title: Interpolation.expression drops trailing whitespace despite documentation saying it includes any whitespace · Issue #154719 · python/cpython · GitHub
Open Graph Title: Interpolation.expression drops trailing whitespace despite documentation saying it includes any whitespace · Issue #154719 · python/cpython
X Title: Interpolation.expression drops trailing whitespace despite documentation saying it includes any whitespace · Issue #154719 · python/cpython
Description: Bug report Bug description: For t-string literals, Interpolation.expression preserves whitespace after the opening {, but removes whitespace immediately before the closing }. This appears inconsistent with the string.templatelib document...
Open Graph Description: Bug report Bug description: For t-string literals, Interpolation.expression preserves whitespace after the opening {, but removes whitespace immediately before the closing }. This appears inconsist...
X Description: Bug report Bug description: For t-string literals, Interpolation.expression preserves whitespace after the opening {, but removes whitespace immediately before the closing }. This appears inconsist...
Opengraph URL: https://github.com/python/cpython/issues/154719
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Interpolation.expression drops trailing whitespace despite documentation saying it includes any whitespace","articleBody":"# Bug report\n\n### Bug description:\n\nFor t-string literals, `Interpolation.expression` preserves whitespace after the opening `{`, but removes whitespace immediately before the closing `}`.\n\nThis appears inconsistent with the `string.templatelib` documentation, which states that, for interpolations created from t-string literals, expression contains the expression text inside the curly braces, including any whitespace.\n\n## Minimal reproducer\n\n```python\nx = 42\n\ncases = [\n t\"{x}\",\n t\"{x }\",\n t\"{ x}\",\n t\"{ x }\",\n t\"{ x }\",\n t\"\"\"{\n x\n}\"\"\",\n]\n\nfor template in cases:\n print(repr(template.interpolations[0].expression))\n```\n\n## Actual behavior\n\nOn CPython main commit b86a41c:\n```text\n'x'\n'x'\n' x'\n' x'\n' x'\n'\\n x'\n```\nWhitespace following { is preserved, but whitespace before } is removed.\n\n## Expected behavior\n\nBased on the current documentation, I expected:\n```text\n'x'\n'x '\n' x'\n' x '\n' x '\n'\\n x\\n'\n```\nIn particular:\n```text\nt\"{x }\".interpolations[0].expression == \"x \"\nt\"{ x }\".interpolations[0].expression == \" x \"\nt\"{ x }\".interpolations[0].expression == \" x \"\n```\n\n## Possible cause\n\nA possible cause is the way the lexer reconstructs interpolation source text in `Parser/lexer/string.c`.\n\n`_PyLexer_update_ftstring_expr()` records `last_expr_end` when it reaches the closing `}`, `!`, or `:` token:\n```c\ncase '}':\ncase '!':\n tok_mode->last_expr_end = strlen(tok->start);\n break;\ncase ':':\n if (tok_mode->last_expr_end == -1) {\n tok_mode->last_expr_end = strlen(tok->start);\n }\n break;\n```\n`_PyLexer_set_ftstring_expr()` then creates the metadata from:\n```c\ntok_mode->last_expr_buffer,\ntok_mode->last_expr_size - tok_mode->last_expr_end\n```\nThis appears to remove the suffix represented by `last_expr_end`. In the ordinary `}` case, that suffix seems to include whitespace immediately before the closing brace, while whitespace immediately after the opening brace remains in `last_expr_buffer`.\n\nThat would explain the observed asymmetry:\n```text\nt\"{ x }\" -> \" x\"\n\nleading whitespace preserved\ntrailing whitespace removed\n```\nThis is only a proposed cause based on the observed behavior and a source-code review.\n\n### CPython versions tested on:\n\n3.16, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154736\n* gh-154762\n* gh-157392\n* gh-157394\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kid-lxy","@type":"Person","name":"kid-lxy"},"datePublished":"2026-07-26T07:26:13Z","url":"https://github.com/python/cpython/issues/154719"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:db657e16-d639-5325-6ad1-8e62e7e80c2f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AAC:2270F4:36F3195:48DB2CB:6B020795 |
| html-safe-nonce | a961b69964f4ff5e538a265f08311f6b3be56c36514540c80894f8c2c1582d59 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUFDOjIyNzBGNDozNkYzMTk1OjQ4REIyQ0I6NkIwMjA3OTUiLCJ2aXNpdG9yX2lkIjoiMTE0MTc5MDYwNjM3Mzg3NzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6240dc78dbd378e5fc71f7b07163716a12d19ee946d546350ab50c9f482c5635 |
| hovercard-subject-tag | issue:4979585664 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/154719/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7c729f6f5dea4c3b1e761cf12055df71c3a603210422031748d73efb1de3d3d0/python/cpython/issues/154719 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7c729f6f5dea4c3b1e761cf12055df71c3a603210422031748d73efb1de3d3d0/python/cpython/issues/154719 |
| og:image:alt | Bug report Bug description: For t-string literals, Interpolation.expression preserves whitespace after the opening {, but removes whitespace immediately before the closing }. This appears inconsist... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kid-lxy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width