Skip to content

Split str.format below the cognitive-complexity threshold (0061) - #382

Merged
rmorozov merged 2 commits into
masterfrom
claude/project-thread-vwcz20
Oct 4, 2026
Merged

rmorozov merged 2 commits into
masterfrom
claude/project-thread-vwcz20

Conversation

@rmorozov

@rmorozov rmorozov commented Oct 4, 2026

Copy link
Copy Markdown
Member

Requested by Ruslan · project thread

Before: StrOps<CharT>::Format in src/value_methods.cpp implements "...".format(...). One function scanned the format string, parsed each replacement field, looked up the argument, applied the .attr and [key] accessors and the !r/!s conversion. Its cognitive complexity was 88 (threshold 25), and it was kept under a NOLINT marker.

After: Format only scans for braces and hands each {...} to ReplaceField. ReplaceField uses two helpers: LookupArg, which resolves a position or keyword and enforces Python's no-mixing rule for {} and {0} through a FieldNumbering, and ApplyAccessors. The brace scan now uses a single doubled flag for both {{ and }}. Output, error messages and which error is reported first are all unchanged. 28 complexity markers remain. Part of docs/tasks/0061.

Roles and rounds: main session plus one verifier round, which found nothing blocking.

  • The verifier rendered about 560k format strings through the parent and through this commit, narrow and wide, with six argument sets each. The strings were 200k structured or random, 250k random, and an exhaustive set of every string up to length 5 over {}!:.[]0rx. The output was byte-identical, every error path included.
  • An ASan+UBSan build was clean, and so were clang-tidy and clang-format.

Red pushes: 0 so far.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LJv5o6nbcd16qaXtwhoNVG


Generated by Claude Code

StrOps::Format scanned the format string, parsed each replacement field,
looked up the argument, applied attribute/index accessors and the
conversion in one function (score 88). Each field is now ReplaceField,
with LookupArg and ApplyAccessors as helpers. Behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJv5o6nbcd16qaXtwhoNVG
@rmorozov rmorozov self-assigned this Oct 4, 2026
@rmorozov
rmorozov marked this pull request as ready for review October 4, 2026 03:57
@rmorozov
rmorozov merged commit 6e82ae9 into master Oct 4, 2026
39 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