#4225·rich

[BUG] Traceback rendering fails on ParseError with a non-string message

Author: saitakarcesmeCreated Sep 22, 2026Updated Sep 27, 2026
  • Checked the traceback documentation and searched open/closed issues for SyntaxError, ParseError, and ExpatError.
  • Checked the FAQ.

Describe the bug

Rendering an xml.etree.ElementTree.ParseError whose message is an ExpatError raises a secondary TypeError, masking the original XML parse error. This occurs with the pure-Python ElementTree parser used by defusedxml (see tiran/defusedxml#105). The reproduction below needs only Rich and the standard library:

python
import io
import sys
from xml.etree.ElementTree import ParseError
from xml.parsers.expat import ExpatError

from rich.console import Console
from rich.traceback import Traceback

try:
    raise ParseError(ExpatError("syntax error"))
except ParseError:
    Console(file=io.StringIO()).print(Traceback.from_exception(*sys.exc_info()))

Actual result:

TypeError: str or Text instance required, not ExpatError('syntax error')

Expected: render the original exception without raising a new exception.

ParseError subclasses SyntaxError. Traceback.extract() currently stores exc_value.msg directly in _SyntaxError, although it already has a safe_str() helper for the main exception value. Would normalizing this field with that helper, with tests for non-string messages and a failing __str__, be an acceptable fix?

Platform

macOS arm64, CPython 3.13.5, Rich 15.0.0. Also reproduced using the current main checkout. Output is directed to StringIO, so no terminal-specific rendering is involved. The original end-to-end example also fails with defusedxml 0.8.0rc2 and structlog 26.1.0. Explicitly selecting structlog.dev.plain_traceback is a verified workaround for that integration.

This report and local reproduction were prepared with OpenAI Codex. Per the AI policy, no PR is being submitted without maintainer approval of the proposed solution.