Repository navigation
Conversation
The comparison used >= instead of >, so a string exactly at maxlen was
'truncated' and came back longer than the input with the suffix
appended: truncate('hello', maxlen=5) returned 'hello...'. Only
truncate strings that actually exceed the limit.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The docstring inaccurately claims results never exceed maxlen; clarify the suffix-overhead behavior.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Fixes truncate so strings exactly at maxlen are returned unchanged.
Changes:
- Changes the boundary check from
>=to>. - Adds regression tests for exact-fit and truncation behavior.
- Updates the function documentation.
| File | Summary |
|---|---|
celery/utils/text.py |
Corrects truncation behavior and updates documentation. |
t/unit/utils/test_text.py |
Adds boundary-condition regression coverage. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| """Truncate text to a maximum number of characters. | ||
|
|
||
| The result never exceeds ``maxlen`` characters including the suffix; | ||
| strings that already fit are returned unchanged. | ||
| """ |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10797 +/- ##
=======================================
Coverage 90.20% 90.20%
=======================================
Files 153 153
Lines 21052 21052
Branches 2540 2540
=======================================
Hits 18990 18990
Misses 1755 1755
Partials 307 307
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Thanks for the fix. I checked it out and ran it against main.
The change is correct
len(s) >= maxlen truncated strings that were exactly maxlen long, so the output grew instead of shrinking. Results I got:
| Call | main | this PR |
|---|---|---|
truncate("hello", 5) |
'hello...' (8 chars) |
'hello' |
truncate("hello world", 11) |
'hello...' |
'hello world' |
truncate("a" * 128, 128) |
131 chars | 128 chars |
truncate("hello!", 5) |
'hello...' |
'hello...' (unchanged) |
The second row is the more visible symptom: on main a string with a space that fits exactly loses its last word. The new test fails on main and passes here. The unit tests for the callers of truncate (saferepr, canvas repr, the consumer and pool logging) pass unchanged.
One thing to fix before merge: the docstring is not accurate
The result never exceeds
maxlencharacters including the suffix
That is not what the function does, before or after this PR. maxlen limits the text that is kept, and the suffix is added on top of it. The PR's own test shows it: truncate("hello!", maxlen=5) == "hello..." is 8 characters for maxlen=5. Suggested wording:
"""Truncate text to a maximum number of characters.
Strings of up to ``maxlen`` characters are returned unchanged.
Longer strings are cut to at most ``maxlen`` characters, at the last
space if there is one, and ``suffix`` is appended, so the result
can be up to ``len(suffix)`` characters longer than ``maxlen``.
"""Minor, optional
- The new cases would fit in the existing parametrized
test_truncate_textjust above, instead of a separate test function. - A case with a space at the boundary, for example
("hello world", 11, "hello world"), would cover the word-dropping symptom, which the current cases do not.
With the docstring corrected this looks good to merge from my side.

Summary
truncatecompares with>=, so a string exactly atmaxlenis needlessly "truncated" — and the result is longer than the input:The
>=should be>: a string that already fits the limit must be returned as-is. (Suffix-on-top-of-prefix semantics for genuinely longer strings are unchanged and covered by the existing parametrized tests.)Fix
AI Disclosure
This PR was prepared with the assistance of
ZCode (GLM-based coding agent): the growth-at-exact-fit behavior was found by probingcelery.utils.text, and the fix, regression test, and description were drafted by the tool. The change was reviewed and tested by the author, who is responsible for it.