Skip to content

✨ Suppress modules in Rich tracebacks with pretty_exceptions_suppress#1498

Draft
harkabeeparolus wants to merge 14 commits intofastapi:masterfrom
harkabeeparolus:feature/traceback_suppress
Draft

✨ Suppress modules in Rich tracebacks with pretty_exceptions_suppress#1498
harkabeeparolus wants to merge 14 commits intofastapi:masterfrom
harkabeeparolus:feature/traceback_suppress

Conversation

@harkabeeparolus
Copy link

It would be useful to provide our users with the Rich traceback suppress parameter. This would allow developers to suppress uninteresting stack frames in tracebacks from other Python frameworks.

For example, python-github, which gives a verbose traceback with many frames on a simple GitlabAuthenticationError. I would like to be able to suppress this entire call stack, but it doesn't work:

#! /usr/bin/env -S uv run --script

# /// script
# dependencies = [ "python-gitlab", "typer", "rich" ]
# ///

import gitlab
import rich.traceback
import typer

# This code does nothing; Typer will override it
rich.traceback.install(suppress=[gitlab])

def main():
    gl = gitlab.Gitlab()
    print(gl.pagesdomains.list())

typer.run(main)

But with this pull request, you can suppress modules directly with Typer():

#! /usr/bin/env -S uv run --script

# /// script
# dependencies = [ "python-gitlab", "typer" ]
# ///

import gitlab
import typer

app = typer.Typer(pretty_exceptions_suppress=[gitlab])

@app.command()
def main():
    gl = gitlab.Gitlab()
    print(gl.pagesdomains.list())

app()

@harkabeeparolus harkabeeparolus changed the title New Option to suppress modules in Rich tracebacks 💡 Suppress modules in Rich tracebacks Jan 23, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 23, 2026

@svlandeg svlandeg added the feature New feature, enhancement or request label Jan 23, 2026
@svlandeg

This comment was marked as resolved.

@svlandeg svlandeg marked this pull request as draft January 23, 2026 13:20
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 10, 2026
@github-actions

This comment was marked as resolved.

@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 11, 2026
@harkabeeparolus harkabeeparolus marked this pull request as ready for review February 11, 2026 11:12
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Feb 17, 2026
@github-actions

This comment was marked as resolved.

@svlandeg svlandeg self-assigned this Feb 17, 2026
…_suppress

# Conflicts:
#	typer/main.py
#	typer/models.py
@github-actions github-actions bot removed the conflicts Automatically generated when a PR has a merge conflict label Feb 18, 2026
@harkabeeparolus

This comment was marked as resolved.

@svlandeg
Copy link
Member

Thanks @harkabeeparolus! I'll try to review this week or the next 🙏

@svlandeg svlandeg changed the title 💡 Suppress modules in Rich tracebacks ✨ Suppress modules in Rich tracebacks with pretty_exceptions_suppress Feb 25, 2026
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I think the feature request in itself might make sense, though I'd have to double check with Tiangolo whether he'd want this or not.

Unfortunately the implementation in this PR is not good enough, as it uses a mock-up lib that users won't have in their env, and the test is not actually functional.


## Disable Tracebacks From Certain Modules

If you are developing with Python frameworks other than **Typer** and **Click**,
Copy link
Member

Choose a reason for hiding this comment

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

As we'll be vendoring Click in the near future, I suggest to not include the reference here.

Suggested change
If you are developing with Python frameworks other than **Typer** and **Click**,
If you are developing with other Python frameworks besides **Typer**,

@@ -0,0 +1,18 @@
import gitlab
Copy link
Member

Choose a reason for hiding this comment

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

I see that you're mocking the installation of gitlab for this to run in the test suite.

What I dislike about using gitlab as an example here, is that users reading the tutorial would have to install the lib just to replicate this one example.

Is there a different example, with already installed libraries, you can think of to showcase this functionality?


</div>

## Disable Tracebacks From Certain Modules
Copy link
Member

Choose a reason for hiding this comment

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

Similar to the other sections in this part of the tutorial, this section should show the output before and after setting this new var.

Comment on lines +10 to +13

# This will raise an exception if not authenticated:
# GitlabAuthenticationError: 401: 401 Unauthorized
# But the traceback will not show any lines from the gitlab module!
Copy link
Member

Choose a reason for hiding this comment

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

In general we tend to avoid comments in the source files, as they are displayed verbatim in the tutorial, which should have all the relevant information.

Suggested change
# This will raise an exception if not authenticated:
# GitlabAuthenticationError: 401: 401 Unauthorized
# But the traceback will not show any lines from the gitlab module!

_gitlab.__file__ = "gitlab/__init__.py" # type: ignore[attr-defined]
sys.modules.setdefault("gitlab", _gitlab)

from docs_src.exceptions import tutorial005_py310 as mod # noqa: E402
Copy link
Member

Choose a reason for hiding this comment

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

There's no need for noqa: E402 here, just move the import to the top of the file.

# that the tutorial code runs without errors.

# Perhaps we could find some standard library module that throws a long
# traceback and test that instead, but for now this is probably good enough.
Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid I disagree - this test doesn't actually test anything. What should get tested, is whether or not the traceback is suppressed. Switching to an actual lib that is in the environment (instead of doing all this mocking) would probably resolve that.

@svlandeg svlandeg removed their assignment Feb 25, 2026
@harkabeeparolus
Copy link
Author

Thank you for the review! @svlandeg ☺️

I see your point about not testing the actual suppression of traceback content. I thought that since this is a standard Rich feature, we'd effectively be testing whether internal Rich features are working or not. And I thought that would be Rich's responsibility.

However, since Typer de facto breaks this standard Rich feature (by overriding it), it would make sense to test what actually happens to the end user. Fair enough. 😊

I'll try to Google or brainstorm some kind of scenario with a long traceback stack through the Python standard library, I guess. 🤔

@harkabeeparolus
Copy link
Author

I think I found a good use case, with only Python standard library modules. With an unknown URL protocol you get five (!) stack frames from urllib.request. 😳

Try this one with and without my PR, to really see how much mess the default Rich traceback formatter normally can remove in these scenarios:

import urllib.request

import typer

app = typer.Typer(pretty_exceptions_suppress=[urllib.request])


@app.command()
def main():
    urllib.request.urlopen("unknown://example.com")


app()

I'll look into updating my PR with this scenario instead of python-gitlab in the next few days, so we can actually test the business logic. And add a before-and-after that could be used to frighten small children. 😱 I think it'll be an improvement. ☺️

@github-actions github-actions bot removed the waiting label Feb 25, 2026
@svlandeg
Copy link
Member

Sounds great!

@svlandeg svlandeg marked this pull request as draft February 25, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature, enhancement or request waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants