Skip to content

docs(text): document whitespace-stripped indexing in Text#4866

Merged
chopan050 merged 3 commits into
ManimCommunity:mainfrom
SupernovaIa:docs/text-index-whitespace-caveat
Jul 14, 2026
Merged

docs(text): document whitespace-stripped indexing in Text#4866
chopan050 merged 3 commits into
ManimCommunity:mainfrom
SupernovaIa:docs/text-index-whitespace-caveat

Conversation

@SupernovaIa

Copy link
Copy Markdown
Contributor

Summary

Text strips whitespace/newlines from its internal self.text after construction, and never creates submobjects for whitespace characters (nothing to render). Since Text doesn't override __getitem__, slicing (text[a:b]) and the [a:b] slice syntax in t2c/t2s/t2w/t2f/t2g end up indexing into that whitespace-stripped copy — not the string the user actually passed in — with no warning or error.

This PR adds a docstring callout on Text explaining the behavior and pointing to substring-keyed t2c/etc. (matched by text search, unaffected by this) as the recommended alternative when the target substring is known ahead of time.

No behavior changes — docs only.

Fixes #4864

Test plan

  • uv run pre-commit run --files manim/mobject/text/text_mobject.py passes (ruff, mypy, codespell)
  • Text(...) still imports/constructs normally
  • Docstring renders as expected (Text.__doc__ includes the new warning block)

Text.__init__ strips spaces/newlines from self.text and never creates
submobjects for whitespace, so slicing (text[a:b]) and the t2c/t2s/t2w/t2f/t2g
'[a:b]' syntax silently index into that stripped copy rather than the
original string, with no warning. Add a docstring callout explaining this and
pointing to substring-keyed t2c/etc. as the unaffected alternative.

Refs ManimCommunity#4864

@chopan050 chopan050 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the extra documentation!

I just made a local test, however, and the behavior for t2c and similar parameters does consider the whitespace, so the docstring has to mention this.

If text = Text("Hello World"), then, indeed, text[5] refers to the "W", not the whitespace. Thus, text[3:7] gets the 4 letters "l", "o", "W", "o". However, when using t2c={"[3:7]": RED}, this does consider the whitespace and paints "l", "o", "", "W" in red.

In other words:

class ColoringText(Scene):
    def construct(self):
        text = Text("Hello World", t2c={"[3:7]": RED})
        self.play(Write(text))
        self.play(text[3:7].animate.set_color(BLUE))
        self.wait()

starts with the "Hello World" text where "lo W" (without the second "o") is colored in red, and then "lo Wo" (with the second "o") gets colored in blue.

Reviewer (chopan050) pointed out on PR ManimCommunity#4866 that t2c/t2s/t2w/t2f/t2g
slice syntax (e.g. t2c={'[3:7]': RED}) indexes into the original text
with whitespace included, not the whitespace-stripped text as the
previous wording implied -- the opposite of how direct object slicing
(my_text[3:5]) behaves. Verified both behaviors empirically and
reworded the warning to describe each convention correctly.
@SupernovaIa

Copy link
Copy Markdown
Contributor Author

@chopan050 thanks for the careful test! You're right that the two paths disagree, so I've reworked the warning to document both conventions explicitly:

  • Slicing the object itself (my_text[3:7]) indexes the rendered characters (whitespace stripped) → selects "loWo".
  • The slice syntax in t2c/t2s/etc. (t2c={'[3:7]': RED}) indexes the original text argument, whitespace included → colors "l", "o", "W" (the space at index 5 falls in range but has nothing to render).

This matches the Text("Hello World", ...) example you described. Ready for another look whenever you have a moment 🙏

@chopan050 chopan050 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@chopan050 chopan050 merged commit 7bef6c0 into ManimCommunity:main Jul 14, 2026
16 of 17 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.

Text slicing/indexing silently operates on a whitespace-stripped copy of the string, not the original

2 participants