-
Notifications
You must be signed in to change notification settings - Fork 20
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
Customize dataclass representations #27
Comments
To work around A, I did a bit of monkeypatching:
Output:
This is almost right, though for consistency [easier reading, easier copy/paste in different contexts] I'd prefer:
Thanks for a great library! |
You can implement and register your custom prettyprinter for each dataclass you've defined. For example, if the automatic prettyprinter for Const gives from prettyprinter import register_pretty, pretty_call
@register_pretty(Const)
def pretty_const(value, ctx):
return pretty_call(
ctx,
'Const', # this is documented as accepting only a callable, but a string also works
value.value
) Since this registered printer is more specific in the subclass hierarchy, it will be used instead of the generic dataclass printer. For more information, see https://prettyprinter.readthedocs.io/en/latest/usage.html#pretty-printing-your-own-types and the API reference for |
If you want different kinds of indentation than what the library currently offers, I think you'll have to drop down to the layout primitives used internally rather than the much easier to use public functions like https://github.com/tommikaikkonen/prettyprinter/blob/master/prettyprinter/prettyprinter.py#L847 If the layout primitives are not enough to express your wanted indentation style, you'd have to modify the layout algorithm in The layout system and primitives are not the easiest to grok at first, but prettyprinter uses a very similar system as the Prettier library for JavaScript. There should be a lot of practical examples of using the primitives in their codebase in addition to this library. There are links to some whitepapers at the top of |
Description
I work on a compiler for a minilanguage. The worflow depends on printing out the AST at various points in the compilation pipeline, and on copy/pasting said AST as test-case expected values. For grokking purposes, the AST representation should be properly indented. I use a home-baked pprint scheme, but I discovered prettyprinter and I would love to migrate. However, the default representation of dataclasses is too verbose. Is there a good way to customize dataclass representations:
A. A way to pprint short names for dataclasses,
Plus(...)
instead ofpython3.tests.playground.Plus(...)
.B. A way to pprint the arguments of some dataclasses as args instead of kwargs,
Const(1)
instead ofConst(value=1)
C. A way to keep the indentation of a given dataclass consistent, always multiline or always single line, instead of depending on contextual line length.
What I Did
Output:
Desired output:
The text was updated successfully, but these errors were encountered: