Skip to content

[BUG] Traceback handler does supress context #468

@beanaroo

Description

@beanaroo

Describe the bug
PEP 409 allows you to suppress exception context. This does not appear to have an effect after installing the rich traceback handler.

To Reproduce

from rich import traceback
traceback.install()

try:
    foo = os.environ['FOO']
except KeyError:
    raise Exception('Environment not configured. Missing FOO setting.') from None

Output (without handler)

Traceback (most recent call last):
  File "/home/beanaroo/Projects/foo/main.py", line 98, in <module>
    raise Exception('Environment not configured. Missing FOO setting.') from None
Exception: Environment not configured. Missing FOO setting.

Output (with handler)

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/beanaroo/Projects/foo/main.py:676 in __getitem__                                           │
│                                                                                                  │
│    673 │                                                                                         │
│    674 │   def __getitem__(self, key):                                                           │
│    675 │   │   try:                                                                              │
│ ❱  676 │   │   │   value = self._data[self.encodekey(key)]                                       │
│    677 │   │   except KeyError:                                                                  │
│    678 │   │   │   # raise KeyError with the original key value                                  │
│    679 │   │   │   raise KeyError(key) from None                                                 │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
KeyError: b'FOO'

During handling of the above exception, another exception occurred:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/beanaroo/Projects/foo/main.py:96 in <module>                                               │
│                                                                                                  │
│    93 traceback.install()                                                                        │
│    94                                                                                            │
│    95 try:                                                                                       │
│ ❱  96 │   foo = os.environ['FOO']                                                                │
│    97 except KeyError:                                                                           │
│    98 │   raise Exception('Environment not configured. Missing FOO setting.') from None          │
│    99                                                                                            │
│                                                                                                  │
│ /home/mike/.pyenv/versions/3.9.0/lib/python3.9/os.py:679 in __getitem__                          │
│                                                                                                  │
│    676 │   │   │   value = self._data[self.encodekey(key)]                                       │
│    677 │   │   except KeyError:                                                                  │
│    678 │   │   │   # raise KeyError with the original key value                                  │
│ ❱  679 │   │   │   raise KeyError(key) from None                                                 │
│    680 │   │   return self.decodevalue(value)                                                    │
│    681 │                                                                                         │
│    682 │   def __setitem__(self, key, value):                                                    │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
KeyError: 'FOO'

During handling of the above exception, another exception occurred:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /home/beanaroo/Projects/foo/main.py:98 in <module>                                               │
│                                                                                                  │
│    95 try:                                                                                       │
│    96 │   foo = os.environ['FOO']                                                                │
│    97 except KeyError:                                                                           │
│ ❱  98 │   raise Exception('Environment not configured. Missing FOO setting.') from None          │
│    99                                                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
Exception: Environment not configured. Missing FOO setting.

Alternative

from rich import traceback
traceback.install()

try:
    bar = os.environ['BAR']
except KeyError as err:
    raise Exception('A problem occurred when setting up bar') from err

Expected Output

The above exception was the direct cause of the following exception:

Actual Output

During handling of the above exception, another exception occurred:

Platform
Python 3.9.0 venv on Arch Linux with Rich 9.3.0 using any terminal

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions