Skip to content

fix(text): stop truncate from cutting strings that already fit - #10797

Open
simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:sq/truncate-suffix-overrun
Open

simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:sq/truncate-suffix-overrun

Conversation

@simpleqt

@simpleqt simpleqt commented Oct 4, 2026

Copy link
Copy Markdown

Summary

truncate compares with >=, so a string exactly at maxlen is needlessly "truncated" — and the result is longer than the input:

from celery.utils.text import truncate

truncate("hello", maxlen=5)    # -> 'hello...'  (8 chars, input was 5!)
truncate("hello", maxlen=4)    # -> 'hell...'   (expected: truncate)
truncate("hello", maxlen=6)    # -> 'hello'     (fits, unchanged)

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

if maxlen and len(s) > maxlen:
    return s[:maxlen].rsplit(' ', 1)[0] + suffix
return s

AI Disclosure

This PR was prepared with the assistance of ZCode (GLM-based coding agent): the growth-at-exact-fit behavior was found by probing celery.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.

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.
Copilot AI lite review requested due to automatic review settings October 4, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The docstring inaccurately claims results never exceed maxlen; clarify the suffix-overhead behavior.

Review effort: Lite
Findings: 1 Low severity

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.

Comment thread celery/utils/text.py
Comment on lines +90 to +94
"""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

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.20%. Comparing base (1490f6d) to head (337859b).

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           
Flag Coverage Δ
unittests 90.18% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@auvipy auvipy added this to the 5.7.x milestone Oct 4, 2026

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 maxlen characters 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_text just 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.

@auvipy
auvipy self-requested a review October 5, 2026 08:53

This branch has not been deployed

No deployments
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.

4 participants