Skip to content

Conversation

@cxzhong
Copy link
Contributor

@cxzhong cxzhong commented Oct 13, 2025

Since IPython 9.6 (with Python 3.12+), the traceback formatter in IPython.core.ultratb.ListTB._format_exception_only() introduced a stricter assertion that requires SyntaxError.text to be a string.

When interface cell magics are invoked with invalid options (e.g., '%%gap foo'), Sage's InterfaceMagic.cell_magic() raised a SyntaxError without setting the .text attribute. This caused IPython 8.27+ to trigger an additional AssertionError during traceback rendering:

    AssertionError: assert isinstance(value. Text, str)

This fix ensures that the SyntaxError has a properly formatted .text attribute set before raising it, preventing the assertion failure and providing better error messages.

The fix also includes a regression test verifying that the .text attribute is correctly set.

Fix errors raised in #41036 https://github.com/sagemath/sage/actions/runs/18470853388

you can refer to ipython/ipython@db0fde0

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

Since IPython 8.27 (with Python 3.12+), the traceback formatter in
IPython.core.ultratb.ListTB._format_exception_only() introduced a
stricter assertion that requires SyntaxError.text to be a string.

When interface cell magics are invoked with invalid options (e.g.,
'%%gap foo'), Sage's InterfaceMagic.cell_magic() raised a SyntaxError
without setting the .text attribute. This caused IPython 8.27+ to
trigger an additional AssertionError during traceback rendering:

    AssertionError: assert isinstance(value.text, str)

This fix ensures that the SyntaxError has a properly formatted .text
attribute set before raising it, preventing the assertion failure and
providing better error messages.

The fix also includes a regression test verifying that the .text
attribute is correctly set.

Related IPython changes:
- ipython/ipython#14764
- ipython/ipython#14770
@cxzhong cxzhong marked this pull request as ready for review October 13, 2025 17:15
Copilot AI review requested due to automatic review settings October 13, 2025 17:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a compatibility issue with IPython 8.27+ where InterfaceMagic would raise incomplete SyntaxError exceptions that triggered assertion failures in IPython's traceback formatter.

  • Ensures SyntaxError objects have a properly formatted .text attribute before being raised
  • Adds a doctest to verify the .text attribute is correctly set on SyntaxError exceptions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@github-actions
Copy link

Documentation preview for this PR (built with commit 61d1c5e; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@cxzhong cxzhong changed the title Fix InterfaceMagic compatibility with IPython >= 8.27 Fix InterfaceMagic compatibility with IPython >= 9.6 Oct 13, 2025
@cxzhong cxzhong closed this Oct 13, 2025
@cxzhong
Copy link
Contributor Author

cxzhong commented Oct 13, 2025

seems @tobiasdiez has reported and will fix in next version.

We need to skip version 9.6

@cxzhong cxzhong deleted the bugfix/interface_magic-ipython-assertion branch October 13, 2025 20:01
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.

1 participant