tommikaikkonen / prettyprinter

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
https://prettyprinter.readthedocs.io
MIT License
336 stars 20 forks source link

`requests.structures.CaseInsensitiveDict` does not format correctly #32

Closed carlwgeorge closed 5 years ago

carlwgeorge commented 5 years ago

Description

requests.structures.CaseInsensitiveDict does not format correctly with either pprint or cpprint.

What I Did

>>> import prettyprinter, requests
>>> r = requests.get('https://httpbin.org')
>>> type(r.headers)
<class 'requests.structures.CaseInsensitiveDict'>
>>> prettyprinter.pprint(r.headers)
{'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Date': 'Fri, 15 Feb 2019 16:46:54 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '10122', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}

One can work around it by wrapping the CaseInsensitiveDict with dict(), but it would be nice if this wasn't necessary.

>>> prettyprinter.pprint(dict(r.headers))
{
    'Connection': 'keep-alive',
    'Server': 'gunicorn/19.9.0',
    'Date': 'Fri, 15 Feb 2019 16:46:54 GMT',
    'Content-Type': 'text/html; charset=utf-8',
    'Content-Length': '10122',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': 'true',
    'Via': '1.1 vegur'
}
tommikaikkonen commented 5 years ago

The requests pretty printer is not enabled by default. Run this:

prettyprinter.install_extras(['requests'])

in the shell and then prettyprinter.pprint(r.headers) and you should have the correct output.

I personally have IPython run a script on startup that sets all the prettyprinter extras and settings I need. If you're using the normal shell, there are some instructions here on how to do that: https://prettyprinter.readthedocs.io/en/latest/usage.html#usage-in-the-default-python-shell

One could make a case for the requests extra to be enabled by default, which would definitely be useful. However, I made the decision to leave all extras opt-in. If the extras get out of date and/or break, or they omit some useful information that the library's own __repr__ method now provides, it's cumbersome to disable pretty printers. And I haven't added any functionality to deregister a pretty printer, which makes it extra cumbersome :)

carlwgeorge commented 5 years ago

Ah I didn't realize that. Based on this line in the README I thought that it should work.

requests - automatically pretty prints Requests, Responses, Sessions, and more from the requests library

Are the docs out of date, or do they just need to be elaborated on (some requests types work by default, others need an extra step)?

Also, I'm not seeing a clear explanation in the docs of install_extras and what it does. Could that be added?

tommikaikkonen commented 5 years ago

I have clarified in the docs that extras need to be enabled - see https://github.com/tommikaikkonen/prettyprinter/commit/57ef4f25c3ef462ec21338b46c17d4c7c9abe747.

There is an API reference entry for install_extras but it doesn't communicate much more than the function name. I'll expand on that a bit in the next release.

carlwgeorge commented 5 years ago

Awesome, thanks!