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] print does not work well with DefaultDict #2988

Closed
2 tasks done
ZhiyuanChen opened this issue Jun 3, 2023 · 6 comments
Closed
2 tasks done

[BUG] print does not work well with DefaultDict #2988

ZhiyuanChen opened this issue Jun 3, 2023 · 6 comments
Labels
Can't reproduce Issue could not be reproduced

Comments

@ZhiyuanChen
Copy link

ZhiyuanChen commented Jun 3, 2023

Describe the bug

rich.print inspects __rich__ and aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf of object during printing.

However, it does not inspect if the __getattr__ of object will always returns something.

ipython avoids this behaviour by checking _ipython_canary_method_should_not_exist_ before inspecting the object.

from chanfig import Config
from rich import print as pprint


if __name__ == '__main__':
    config = Config(**{'hello': 'world'})
    print('print', config)
    pprint('rich.print', config)
    print(config.__rich__)
    print(config.keys())
rint Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)
rich.print Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
  ('__rich__'): Config(<class 'chanfig.config.Config'>, )
  ('aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf'): Config(<class 'chanfig.config.Config'>, )
)
Config(<class 'chanfig.config.Config'>, )
dict_keys(['hello', '__rich__', 'aihwerij235234ljsdnp34ksodfipwoe234234jlskjdf'])

ref: ZhiyuanChen/CHANfiG#6

@github-actions
Copy link

github-actions bot commented Jun 3, 2023

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

I can't reproduce this with recent Rich:

print Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)
rich.print Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)
<bound method FlatDict.__rich__ of Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)>
dict_keys(['hello'])

@willmcgugan willmcgugan added Can't reproduce Issue could not be reproduced and removed Needs triage labels Jul 29, 2023
@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

@ZhiyuanChen
Copy link
Author

I can't reproduce this with recent Rich:

print Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)
rich.print Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)
<bound method FlatDict.__rich__ of Config(<class 'chanfig.config.Config'>,
  ('hello'): 'world'
)>
dict_keys(['hello'])

Yes, since this issue wasn't responded in a long period, I did some hack in chanfig as a temporary solution. See more at ZhiyuanChen/CHANfiG@3155769.

However, this is still an issue. rich will have same behaviour when working with python's collection.defaultdict.

image

@willmcgugan
Copy link
Collaborator

I'm going to need a minimal reproducible example, because Rich does work with default dict as you can see here:

>>> from collections import defaultdict
>>> d=defaultdict(str)
>>> d["foo"] = "bar"
>>> from rich import print
>>> print(d)
defaultdict(<class 'str'>, {'foo': 'bar'})

@ZhiyuanChen
Copy link
Author

I'm going to need a minimal reproducible example, because Rich does work with default dict as you can see here:

>>> from collections import defaultdict

>>> d=defaultdict(str)

>>> d["foo"] = "bar"

>>> from rich import print

>>> print(d)

defaultdict(<class 'str'>, {'foo': 'bar'})

Thank you. Could you please try use chanfig <= 0.0.81?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can't reproduce Issue could not be reproduced
Projects
None yet
Development

No branches or pull requests

2 participants