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

fix pretty_repr for dataclasses in frozen bundles (e.g. pyinstaller) #3592

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fixed pretty_repr for dataclasses in frozen (e.g. pyinstaller) bundles https://github.com/Textualize/rich/pull/3592

## [13.9.4] - 2024-11-01

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following people have contributed to the development of Rich:
- [Hugo van Kemenade](https://github.com/hugovk)
- [Andrew Kettmann](https://github.com/akettmann)
- [Alexander Krasnikov](https://github.com/askras)
- [Talley Lambert](https://github.com/tlambert03)
- [Martin Larralde](https://github.com/althonos)
- [Hedy Li](https://github.com/hedythedev)
- [Henry Mai](https://github.com/tanducmai)
Expand Down
11 changes: 7 additions & 4 deletions rich/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def _get_attr_fields(obj: Any) -> Sequence["_attr_module.Attribute[Any]"]:
return _attr_module.fields(type(obj)) if _has_attrs else []


IS_FROZEN = getattr(sys, "frozen", False)


def _is_dataclass_repr(obj: object) -> bool:
"""Check if an instance of a dataclass contains the default repr.

Expand All @@ -79,10 +82,10 @@ def _is_dataclass_repr(obj: object) -> bool:
# Digging in to a lot of internals here
# Catching all exceptions in case something is missing on a non CPython implementation
try:
return obj.__repr__.__code__.co_filename in (
dataclasses.__file__,
reprlib.__file__,
)
accepted = {dataclasses.__file__, reprlib.__file__}
if IS_FROZEN:
accepted.update({"dataclasses.py", "reprlib.py"})
return obj.__repr__.__code__.co_filename in accepted
except Exception: # pragma: no coverage
return False

Expand Down