-
-
Notifications
You must be signed in to change notification settings - Fork 852
✨ Suppress modules in Rich tracebacks with pretty_exceptions_suppress
#1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
77ee838
fffa6ee
dc55834
41ce825
8e2cd62
e2d539d
20b19c5
b4fd484
4ccfe2b
a3260ec
e7e5ef7
667b16d
4f0f195
a2f39df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -192,6 +192,27 @@ $ python main.py | |||||
|
|
||||||
| </div> | ||||||
|
|
||||||
| ## Disable Tracebacks From Certain Modules | ||||||
|
|
||||||
| If you are developing with Python frameworks other than **Typer** and **Click**, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
| you might get very verbose tracebacks, which could make it difficult to find the | ||||||
| line in your own code that triggered the exception. | ||||||
|
|
||||||
| With pretty exceptions, you can use the parameter `pretty_exceptions_suppress`, | ||||||
| which takes a list of Python modules, or `str` paths, to indicate which modules | ||||||
| should have their traceback frames suppressed by the **Rich** traceback | ||||||
| formatter. Only filename and line number will be shown for these modules, but no | ||||||
| code or variables. ⚡ | ||||||
|
|
||||||
| For example, if you are developing a GitLab utility using the `python-gitlab` | ||||||
| package, you might notice that tracebacks are very long and filled with internal | ||||||
| calls inside the `gitlab` module that you probably do not care about. In this case, | ||||||
| you can suppress the traceback frames inside the `gitlab` module: | ||||||
|
|
||||||
| {* docs_src/exceptions/tutorial005_py310.py hl[4] *} | ||||||
|
|
||||||
| And now you can see clearly which of your calls to `gitlab` caused the exception. 💡 | ||||||
|
|
||||||
| ## Disable Pretty Exceptions | ||||||
|
|
||||||
| You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`: | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||
| import gitlab | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you're mocking the installation of What I dislike about using Is there a different example, with already installed libraries, you can think of to showcase this functionality? |
||||||||||
| import typer | ||||||||||
|
|
||||||||||
| app = typer.Typer(pretty_exceptions_suppress=[gitlab]) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @app.command() | ||||||||||
| def main(): | ||||||||||
| gitlab_client = gitlab.Gitlab() | ||||||||||
|
|
||||||||||
| # This will raise an exception if not authenticated: | ||||||||||
| # GitlabAuthenticationError: 401: 401 Unauthorized | ||||||||||
| # But the traceback will not show any lines from the gitlab module! | ||||||||||
|
Comment on lines
+10
to
+13
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
| print(gitlab_client.pagesdomains.list()) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| if __name__ == "__main__": | ||||||||||
| app() | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import sys | ||
| from types import ModuleType | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from typer.testing import CliRunner | ||
|
|
||
| # Mock the gitlab module before importing the tutorial | ||
| _gitlab = ModuleType("gitlab") | ||
| _gitlab.Gitlab = MagicMock # type: ignore[attr-defined] | ||
| _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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need for |
||
|
|
||
| runner = CliRunner() | ||
|
|
||
| # There's no way to test a third-party package from PyPI. Also, the actual | ||
| # feature we use is part of Rich. We just pass the flag along. So here we test | ||
| # 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
|
|
||
| def test_pretty_exceptions_suppress(): | ||
| result = runner.invoke(mod.app) | ||
| assert result.exit_code == 0 | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = runner.invoke(mod.app, ["--help"]) | ||
| assert "Usage" in result.stdout | ||
There was a problem hiding this comment.
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.