question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

[BUG] reconfiguring rich via pytest at runtime

See original GitHub issue

Describe the bug

Trying to reconfigure rich when my code is run via pytest, using the following pytest hook:

# conftest.py

def pytest_configure():
    try:
        import rich
    except ImportError:
        pass
    else:
        rich.reconfigure(soft_wrap=False)

and invoking my tests with

pytest

I got the following error:

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/_pytest/main.py", line 265, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/_pytest/config/__init__.py", line 982, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/hooks.py", line 308, in call_historic
INTERNALERROR>     res = self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/manager.py", line 84, in <lambda>
INTERNALERROR>     self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/robcleme/dev/my/wxc/tests/conftest.py", line 33, in pytest_configure
INTERNALERROR>     rich.reconfigure(soft_wrap=False)
INTERNALERROR>   File "/Users/robcleme/.pyenv/versions/wxc-dev/lib/python3.9/site-packages/rich/__init__.py", line 45, in reconfigure
INTERNALERROR>     _console.__dict__ = new_console.__dict__
INTERNALERROR> AttributeError: 'NoneType' object has no attribute '__dict__'

Platform OsX

Diagnose

python -m rich.diagnose
python -m rich._windows
pip freeze | grep rich
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <class 'rich.console.Console'> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ A high level console interface.                                                  โ”‚
โ”‚                                                                                  โ”‚
โ”‚ โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
โ”‚ โ”‚ <console width=185 ColorSystem.TRUECOLOR>                                    โ”‚ โ”‚
โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ โ”‚
โ”‚                                                                                  โ”‚
โ”‚     color_system = 'truecolor'                                                   โ”‚
โ”‚         encoding = 'utf-8'                                                       โ”‚
โ”‚             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ”‚
โ”‚           height = 52                                                            โ”‚
โ”‚    is_alt_screen = False                                                         โ”‚
โ”‚ is_dumb_terminal = False                                                         โ”‚
โ”‚   is_interactive = True                                                          โ”‚
โ”‚       is_jupyter = False                                                         โ”‚
โ”‚      is_terminal = True                                                          โ”‚
โ”‚   legacy_windows = False                                                         โ”‚
โ”‚         no_color = False                                                         โ”‚
โ”‚          options = ConsoleOptions(                                               โ”‚
โ”‚                        size=ConsoleDimensions(width=185, height=52),             โ”‚
โ”‚                        legacy_windows=False,                                     โ”‚
โ”‚                        min_width=1,                                              โ”‚
โ”‚                        max_width=185,                                            โ”‚
โ”‚                        is_terminal=True,                                         โ”‚
โ”‚                        encoding='utf-8',                                         โ”‚
โ”‚                        max_height=52,                                            โ”‚
โ”‚                        justify=None,                                             โ”‚
โ”‚                        overflow=None,                                            โ”‚
โ”‚                        no_wrap=False,                                            โ”‚
โ”‚                        highlight=None,                                           โ”‚
โ”‚                        markup=None,                                              โ”‚
โ”‚                        height=None                                               โ”‚
โ”‚                    )                                                             โ”‚
โ”‚            quiet = False                                                         โ”‚
โ”‚           record = False                                                         โ”‚
โ”‚         safe_box = True                                                          โ”‚
โ”‚             size = ConsoleDimensions(width=185, height=52)                       โ”‚
โ”‚        soft_wrap = False                                                         โ”‚
โ”‚           stderr = False                                                         โ”‚
โ”‚            style = None                                                          โ”‚
โ”‚         tab_size = 8                                                             โ”‚
โ”‚            width = 185                                                           โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
rich==10.7.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
neutrinoceroscommented, Aug 24, 2021

Do you have the env var COLUMNS set ?

Ah, seems so. I can make my problem go away by setting that in my pytest hook. Good call !

With regards to testing in general though. You may be better off working with Console objects explicitly. That way you have more control over the initial state.

Sure. Right now Iโ€™m trying to live up to the spirit of my tweet https://twitter.com/neutrinoceros/status/1428812031524810756?s=20 This specific project is extremely small so Iโ€™d like to keep the technical debt close to zero, so Iโ€™ll avoid using rich to the fullest for now ๐Ÿ˜ƒ

Also, if you can, check the objects that are being printed, rather than the output. The output is subject to env vars and platform differences.

Well the output is really what I need to check, and rich is only an optional dependency at this point. But Iโ€™ll keep this in mind for other projects where Iโ€™m integrating rich. Thanks !

1reaction
neutrinoceroscommented, Aug 24, 2021

while a possible patch on the rich side is proposed in #1426, a workaround this issue for existing versions of rich is to simply modify the pytest hook as:



def pytest_configure():
    try:
        import rich
    except ImportError:
        pass
    else:
        rich.get_console()  # this is new !
        rich.reconfigure(soft_wrap=False)
Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] Rich is incompatible with pytest capsys fixture #317
I'm having the same issue, I used click.echo and it worked, but now I tried using rich and when I'm looking at the:...
Read more >
pytest Documentation - Read the Docs
You can check that code raises a particular warning using pytest.warns. 2.2.4 Making use of context-sensitive comparisons pytest has rich ...
Read more >
Plugin List โ€” pytest documentation
name summary last release status requi... pytestโ€‘accept A pytestโ€‘plugin for updating doctest outputs Jan 07, 2022 N/A pytes... pytestโ€‘addonsโ€‘test ็”จไบŽๆต‹่ฏ•pytest็š„ๆ’ไปถ Aug 02, 2021 N/A pytes... pytestโ€‘aio...
Read more >
API Reference โ€” pytest documentation
The approx class performs floating-point comparisons using a syntax ... This is because the call hierarchy of rich comparisons follows a fixed behavior....
Read more >
Changelog โ€” pytest documentation
The plan is to make returning non- None from tests an error in the future. ... This is not currently enforced at runtime,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found