Skip to content

Fix conversion of format strings with nested field references#252

Merged
ikamensh merged 1 commit intoikamensh:masterfrom
akx:f-nested
Mar 27, 2026
Merged

Fix conversion of format strings with nested field references#252
ikamensh merged 1 commit intoikamensh:masterfrom
akx:f-nested

Conversation

@akx
Copy link
Copy Markdown
Contributor

@akx akx commented Mar 24, 2026

I first noted, while looking at flynting another project, that

-tr.write('{0:>{1}}'.format(bench[prop], widths[prop]))
+tr.write(f'{bench[prop]:>{ {1} }}')

is a pretty bad conversion.

I wrote a failing test case for that, and then asked Claude Code to fix it.
Turns out the same fix enabled, with a small tweak, conversion of the previously "too complex syntax".

@akx akx marked this pull request as ready for review March 24, 2026 18:19
@akx
Copy link
Copy Markdown
Contributor Author

akx commented Mar 27, 2026

Will fix lint issues.

…at specs

'{0:>{1}}'.format(a, b) was incorrectly producing f'{a:>{{1}}}' (escaped
braces) instead of f'{a:>{b}}'. The format spec was treated as a literal
string, so nested field references like {1} were never resolved.

Parse the format spec for nested fields and build a proper JoinedStr AST
node with resolved references. This also enables converting the previously
skipped "{:{}}".format(x, y) case, as well as named, mixed implicit/explicit,
and multi-field nested specs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ikamensh ikamensh merged commit 9a74f6f into ikamensh:master Mar 27, 2026
10 checks passed
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