Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix crash on prime after single-character superscript - #32066

Merged
tacaswell merged 1 commit into
matplotlib:mainfrom
jameslin9968:fix-mathtext-single-char-superscript-prime
Jul 18, 2026
Merged

Fix crash on prime after single-character superscript#32066
tacaswell merged 1 commit into
matplotlib:mainfrom
jameslin9968:fix-mathtext-single-char-superscript-prime

Conversation

@jameslin9968

@jameslin9968 jameslin9968 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

A prime (apostrophe) following a single-character superscript such as $x^2'$ raised AttributeError: 'Char' object has no attribute 'children'. A single-character superscript is parsed as a bare Char, whereas the apostrophe handling in Parser.subsuper assumed the superscript was an Hlist and called super.children.extend(...). The braced form $x^{2}'$ already produced an Hlist and worked.

The fix wraps a non-Hlist superscript in an Hlist before appending the prime glyphs, matching the braced case.

PR summary

Minimal reproduction (crashes on current main / released matplotlib):

import matplotlib.pyplot as plt
fig = plt.figure()
fig.text(0.1, 0.5, r"$x^2'$")   # prime after single-char superscript
fig.savefig("out.png")          # -> AttributeError: 'Char' object has no attribute 'children'

With this PR the expression renders correctly, matching the already-working braced form $x^{2}'$.

AI Disclosure

This PR was authored with substantial AI assistance (Claude). AI was used to localize the bug in Parser.subsuper, write the fix, and generate the parametrized regression tests in test_mathtext.py. All changes were run and verified locally: the new tests fail without the fix and pass with it, and the full test_mathtext.py suite passes (732 passed / 0 failed).

PR checklist

  • [N/A] "closes #0000" — no pre-existing issue was filed for this crash
  • new and changed code is tested (4 parametrized cases in test_mathtext.py)
  • [N/A] Plotting related features are demonstrated in an example — bugfix, no new feature
  • [N/A] New Features and API Changes are noted in a release note — behavior-only bugfix, none needed
  • Documentation complies with guidelines — no doc changes needed

@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@scottshambaugh

Copy link
Copy Markdown
Contributor

Please fill out the rest of the PR checklist (I see Claude coauthorship but no AI disclosure) and provide a minimal script that demonstrates the issue.

@scottshambaugh scottshambaugh added ai-contribution PRs that are AI generated without a human in the loop status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks labels Jul 18, 2026
@github-actions

This comment has been minimized.

@tacaswell

Copy link
Copy Markdown
Member

Please don't make an issue, the PR is enough.


@jameslin9968 is at the SciPy sprints with us.

@tacaswell

Copy link
Copy Markdown
Member
from matplotlib import pyplot as plt
fig, ax = plt.subplots()

for j, t in enumerate([r"$x^{2}'$", r"$x^{a}'$", r"$x^{2}''$", r"$x^{\alpha}'$"]):
    ax.text(j, .5, t)


fig, ax = plt.subplots()

for j, t in enumerate([r"$x^2'$", r"$x^a'$", r"$x^2''$", r"$x^\alpha'$"]):
    ax.text(j, .5, t)

@scottshambaugh scottshambaugh removed ai-contribution PRs that are AI generated without a human in the loop status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks labels Jul 18, 2026
@jameslin9968

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @scottshambaugh. I've updated the PR description:

  • Filled in the PR checklist.
  • Added an AI Disclosure section — this PR was authored with substantial AI (Claude) assistance, and all changes were verified locally (new tests fail without the fix, pass with it; full test_mathtext.py passes, 732 passed / 0 failed).
  • Added a minimal reproduction script that crashes on current main and released matplotlib.

No pre-existing issue was filed for this crash, so the checklist's "closes #" is marked N/A. Happy to open one if you'd prefer it tracked separately.

@scottshambaugh

scottshambaugh commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

I cannot reproduce this locally on main, what OS / backend is this using?

Edit: Got it reproducing locally.

@scottshambaugh scottshambaugh 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.

My preference would be for the new comments to be trimmed way down, but this looks good to me otherwise.

A prime (apostrophe) following a single-character superscript such as
``$x^2'$`` raised ``AttributeError: 'Char' object has no attribute
'children'``. A single-character superscript is parsed as a bare ``Char``,
whereas the apostrophe handling in ``Parser.subsuper`` assumed the
superscript was an ``Hlist`` and called ``super.children.extend(...)``.
The braced form ``$x^{2}'$`` already produced an ``Hlist`` and worked.

Wrap a non-``Hlist`` superscript in an ``Hlist`` before appending the
prime glyphs, matching the braced case.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jameslin9968
jameslin9968 force-pushed the fix-mathtext-single-char-superscript-prime branch from 0986c14 to 720a33e Compare July 18, 2026 20:21
@jameslin9968

Copy link
Copy Markdown
Contributor Author

Thanks @scottshambaugh, @tacaswell. Trimmed the comments in both the fix and the test down to one line each. Force-pushed.

@tacaswell
tacaswell merged commit 9ad800b into matplotlib:main Jul 18, 2026
41 checks passed
@tacaswell

Copy link
Copy Markdown
Member

Thank you @jameslin9968 and it was great to talk to you at the sprints today. I hope we hear from you agian!

timhoffm added a commit that referenced this pull request Jul 19, 2026
…066-on-v3.11.x

Backport PR #32066 on branch v3.11.x (Fix crash on prime after single-character superscript)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants