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

Short class names #64

Open jjlee opened 5 years ago

jjlee commented 5 years ago

I see this issue has come up before, in https://github.com/tommikaikkonen/prettyprinter/issues/27, so forgive me if you've already considered this and rejected it.

Description

It would be great if we could write something like:

familiar_classes = [mymodule._longname.SomeClass, mymodule._longname.SomeOtherClass]
prettyprinter.configure(short_names=familiar_classes)

And then instead of class names like the following in the output of prettyprinter.pformat:

mymodule._longname.SomeClass(
    identifier='id-1',
    title='Title',
    other=mymodule._longname.SomeOtherClass(),
    other_class=mymodule._longname.SomeOtherClass,
    something=somemodule.Something(),
    ...

I'd want to see instead:

SomeClass(
    identifier='id-1',
    title='Title',
    other=SomeOtherClass(),
    other_class=SomeOtherClass,
    something=somemodule.Something(),
    ...

Sometimes fully qualified class names (with the module name) get pretty long and obscure the content. In my case, most/all of the classes involved are dataclasses, but from the code it looks like that's not relevant.

I realize you can register your own prettyprinters -- which I've also used for one of my classes -- but for this purpose that seems high-effort and perhaps error-prone. I ended up just editing the source code to never print class names as fully qualified, and that's great for what I'm doing right now.

Even nicer (but likely straying into a separate feature request), I'd love to be able to write something like this and not mutate any global state:

pformat = prettyprinter.make_pretty_printer(include=["dataclasses"], short_names=familiar_classes)