allow disable pprint

This commit is contained in:
Will McGugan 2021-11-06 12:16:21 +00:00
parent 6e7d81d8c5
commit dff665e33c
2 changed files with 11 additions and 2 deletions

View File

@ -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 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 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 ## [10.12.0] - 2021-10-06

View File

@ -166,9 +166,17 @@ def install(
ip = get_ipython() # type: ignore ip = get_ipython() # type: ignore
from IPython.core.formatters import BaseFormatter from IPython.core.formatters import BaseFormatter
class RichFormatter(BaseFormatter):
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 # replace plain text formatter with rich formatter
rich_formatter = BaseFormatter() rich_formatter = RichFormatter()
rich_formatter.for_type(object, func=ipy_display_hook)
ip.display_formatter.formatters["text/plain"] = rich_formatter ip.display_formatter.formatters["text/plain"] = rich_formatter
except Exception: except Exception:
sys.displayhook = display_hook sys.displayhook = display_hook