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] Dataclass attributes specified with init=False cause error in pretty printing #3417

Closed
2 tasks done
CollinHeist opened this issue Jul 6, 2024 · 2 comments · Fixed by #3472
Closed
2 tasks done

Comments

@CollinHeist
Copy link

CollinHeist commented Jul 6, 2024

Describe the bug

Objects in pretty traceback printing which are dataclass objects with fields that are specified with init=False will cause an uncaught exception in the printing. See below.

from dataclasses import dataclass, field, fields

@dataclass(eq=False)
class BadDataclass:
    item: int = field(init=False)

# item is not provided
ob = BadDataclass()

# Displays all fields, including item
print([f for f in fields(ob) if f.repr])

# Raises an AttributeError since ob.item is not defined
print([getattr(ob, f.name) for f in fields(ob)])

This is specifically caused by this line of code.

PR Submitted

Copy link

github-actions bot commented Jul 6, 2024

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

CollinHeist added a commit to CollinHeist/rich that referenced this issue Jul 6, 2024
@CollinHeist CollinHeist changed the title [BUG] Dataclass attributes specified with init=False cause error in traceback printing [BUG] Dataclass attributes specified with init=False cause error in pretty printing Jul 6, 2024
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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