Skip to content
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

[BUG] Traceback can't handle to show locals when dataclass fails on __post_init__ and it's not initialized #3339

Open
2 tasks done
gabrielcocenza opened this issue Apr 19, 2024 · 1 comment

Comments

@gabrielcocenza
Copy link

gabrielcocenza commented Apr 19, 2024

Describe the bug

Rich traceback can't handle dataclass that fails on __post_init__ and it's not initialized

Provide a minimal code example that demonstrates the issue if you can. If the issue is visual in nature, consider posting a screenshot.

from typing import Optional
from dataclasses import dataclass, field

@dataclass
class A:
    foo: Optional[str] = field(init=False)

   def __post_init__(self):
       self.foo = self.bar()

   def bar(self):
       raise ValueError("Not valid")

try:
   a = A()
except ValueError:
   console.print_exception(show_locals=True)

This will give the following traceback:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<string>", line 3, in __init__
  File "<stdin>", line 5, in __post_init__
  File "<stdin>", line 7, in bar
ValueError: Not valid

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/home/ubuntu/project/.venv/lib/python3.10/site-packages/rich/console.py", line 1862, in print_exception
    traceback = Traceback(
  File "/home/ubuntu/project/.venv/lib/python3.10/site-packages/rich/traceback.py", line 264, in __init__
    trace = self.extract(
  File "home/ubuntu/project/..venv/lib/python3.10/site-packages/rich/traceback.py", line 449, in extract
    locals={
  File "/home/ubuntu/project/.venv/lib/python3.10/site-packages/rich/traceback.py", line 450, in <dictcomp>
    key: pretty.traverse(
  File "/home/ubuntu/project/.venv/lib/python3.10/site-packages/rich/pretty.py", line 853, in traverse
    node = _traverse(_object, root=True)
  File "/home/ubuntu/project/.venv/lib/python3.10/site-packages/rich/pretty.py", line 767, in _traverse
    child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
AttributeError: 'A' object has no attribute 'foo'

This might be a corner case, but it would be great if rich could support this because in this case the class doesn't have the foo attribute when the exception happens

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?
ubuntu 22.04

Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant