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

allow disable pprint #1647

Merged
merged 2 commits into from
Nov 6, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed an edge case bug when console module try to detect if they are in a tty at the end of a pytest run
- Fixed issue with TERM env vars that have more than one hyphen https://github.com/willmcgugan/rich/issues/1640
- Fixed exception in IPython when disabling pprint with %pprint https://github.com/willmcgugan/rich/issues/1646

## [10.12.0] - 2021-10-06

Expand Down
12 changes: 10 additions & 2 deletions rich/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,17 @@ def ipy_display_hook(value: Any) -> None: # pragma: no cover
ip = get_ipython() # type: ignore
from IPython.core.formatters import BaseFormatter

class RichFormatter(BaseFormatter): # type: ignore
pprint: bool = True

def __call__(self, value: Any) -> Any:
if self.pprint:
return ipy_display_hook(value)
else:
return repr(value)

# replace plain text formatter with rich formatter
rich_formatter = BaseFormatter()
rich_formatter.for_type(object, func=ipy_display_hook)
rich_formatter = RichFormatter()
ip.display_formatter.formatters["text/plain"] = rich_formatter
except Exception:
sys.displayhook = display_hook
Expand Down