Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
[#717](https://github.com/hynek/structlog/pull/717)


### Changed

- `structlog.dev.rich_traceback()` now throws a more helpful error when Rich is missing.
[#735](https://github.com/hynek/structlog/pull/735)


## [25.4.0](https://github.com/hynek/structlog/compare/25.3.0...25.4.0) - 2025-06-02

### Added
Expand Down
25 changes: 17 additions & 8 deletions src/structlog/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,26 @@ def __call__(self, sio: TextIO, exc_info: ExcInfo) -> None:
console.print(tb)


rich_traceback = RichTracebackFormatter()
"""
Pretty-print *exc_info* to *sio* using the Rich package.
if rich is None:

To be passed into `ConsoleRenderer`'s ``exception_formatter`` argument.
def rich_traceback(*args, **kw):
raise ModuleNotFoundError(
"RichTracebackFormatter requires Rich to be installed.",
name="rich",
)

This is a `RichTracebackFormatter` with default arguments and used by default
if Rich is installed.
else:
rich_traceback = RichTracebackFormatter()
"""
Pretty-print *exc_info* to *sio* using the Rich package.

.. versionadded:: 21.2.0
"""
To be passed into `ConsoleRenderer`'s ``exception_formatter`` argument.

This is a `RichTracebackFormatter` with default arguments and used by default
if Rich is installed.

.. versionadded:: 21.2.0
"""


def better_traceback(sio: TextIO, exc_info: ExcInfo) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,19 @@ def test_set_it(self):
assert {"exc_info": True} == dev.set_exc_info(None, "exception", {})


@pytest.mark.skipif(dev.rich is not None, reason="Needs missing Rich.")
def test_rich_traceback_formatter_no_rich():
"""
Trying to use RichTracebackFormatter without Rich should raise an helpful
error.
"""
with pytest.raises(
ModuleNotFoundError,
match="RichTracebackFormatter requires Rich to be installed.",
):
dev.rich_traceback(StringIO(), sys.exc_info())


@pytest.mark.skipif(dev.rich is None, reason="Needs Rich.")
class TestRichTracebackFormatter:
def test_default(self):
Expand Down