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] Can't print MagicMock.mock_calls #903

Closed
henryiii opened this issue Jan 10, 2021 · 4 comments
Closed

[BUG] Can't print MagicMock.mock_calls #903

henryiii opened this issue Jan 10, 2021 · 4 comments

Comments

@henryiii
Copy link
Contributor

Describe the bug
rich.print does not print something that print does, breaking with:

NotRenderableError: Unable to render (<console width=93 ColorSystem.TRUECOLOR>, ConsoleOptions(legacy_windows=False, min_width=1, max_width=93, is_terminal=False, encoding='utf-8', justify=None, overflow=None, no_wrap=False, highlight=None)); A str, Segment or object with __rich_console__ method is required

To Reproduce

from unittest.mock import MagicMock

ListProxy = MagicMock("ListProxy")
y = ListProxy()
y[0] += 1
rich.print(*y.mock_calls)

If you remove the rich. or the *, it works. I'm assuming it is suppose to fall back to repr/str, or is it supposed to break? The list containing it works just fine, just not singled out. This is a fairly unusual thing to print, I know, was working on a tutorial.

Platform

macOS 11 Intel, normally iTerm2 though I was in JupyterLab 3 for this, 9.7.0 from conda-forge.

@willmcgugan
Copy link
Collaborator

Mocks pretty much break introspection. The unittest.mock._Call objects which you are printing claims to have a __rich__ method, so Rich treats it has supporting the Console protocol.

>>> hasattr(y.mock_calls[0], "__rich__")
True

It doesn't support the Rich protocol of course, hassattr will return True for any attribute.

Printing the list works because the contents of the list are repred.

The solution may be to try and detect those mock call objects and special case them.

@willmcgugan willmcgugan changed the title [BUG] [BUG] Can't print MagicMock.mock_calls Jan 11, 2021
@willmcgugan
Copy link
Collaborator

Try v9.8.0 Should be fixed.

@henryiii
Copy link
Contributor Author

henryiii commented Jan 11, 2021

I'll try it as soon as conda-forge/rich-feedstock#47 goes in, thanks! (PS: you can follow the instructions in the bot's PR description to setup auto merge on PRs; if things are not expected to break it makes conda-forge easier to maintain).

@henryiii
Copy link
Contributor Author

It works now, thanks!

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

No branches or pull requests

2 participants