Skip to content

IPythonTracebackLexer errors on back ticks in the presence of line-breaks #14142

Open
ipython/ipython-pygments-lexers
#13

Description

@jakevdp

We've run into this error when rendering notebooks in sphinx documentation via myst-nb. Error messages that include line-breaks and backticks lead to failed code highlighting. The result is a sphinx warning, which in our build configuration leads to a build failure.

Minimal reproduction:

from pygments import highlight
from IPython.lib.lexers import IPythonTracebackLexer
from pygments.formatters import HtmlFormatter
from pygments.filters import RaiseOnErrorTokenFilter

code = "TypeError: `foo` is deprecated.\nUse `bar` instead."
lexer = IPythonTracebackLexer(filters=[RaiseOnErrorTokenFilter()])
print(highlight(code, lexer, HtmlFormatter()))
---------------------------------------------------------------------------
ErrorToken                                Traceback (most recent call last)
[<ipython-input-3-c8ff8ce8d25e>](https://localhost:8080/#) in <cell line: 8>()
      6 code = "TypeError: `foo` is deprecated.\nUse `bar` instead."
      7 lexer = IPythonTracebackLexer(filters=[RaiseOnErrorTokenFilter()])
----> 8 print(highlight(code, lexer, HtmlFormatter()))

8 frames
[/usr/local/lib/python3.10/dist-packages/pygments/filters/__init__.py](https://localhost:8080/#) in filter(self, lexer, stream)
    783         for ttype, value in stream:
    784             if ttype is Error:
--> 785                 raise self.exception(value)
    786             yield ttype, value
    787 

ErrorToken: `

By contrast, the standard PythonTracebackLexer does not have issues with this:

from pygments.lexers import PythonTracebackLexer
lexer = PythonTracebackLexer(filters=[RaiseOnErrorTokenFilter()])
print(highlight(code, lexer, HtmlFormatter()))
<div class="highlight"><pre><span></span><span class="x">TypeError: `foo` is deprecated.</span>
<span class="x">Use `bar` instead.</span>
</pre></div>

Activity

  1. matthew-brett commented on Aug 6, 2026

    @matthew-brett
    Contributor

    We ran into the same problem, one potential workaround at matthew-brett/scipy-lecture-notes@5be6ea5 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions