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

Prettyprinter does not print dataclass default values #74

Open
pynista opened this issue Feb 28, 2020 · 2 comments
Open

Prettyprinter does not print dataclass default values #74

pynista opened this issue Feb 28, 2020 · 2 comments

Comments

@pynista
Copy link

pynista commented Feb 28, 2020

  • PrettyPrinter version: 0.18.0
  • Python version: 3.7.5
  • Operating System: Ubuntu 16.04.6 LTS

Description

Prettyprinter does not print dataclass default values

What I Did

# script.py
from dataclasses import dataclass

import prettyprinter


@dataclass
class Car:
    model: str
    year: int
    owner: str = "Elon Musk"


opel_astra_2002 = Car('Opel Astra', 2002)

prettyprinter.install_extras(
    include=['dataclasses', ],
    warn_on_error=True
)
prettyprinter.pprint(opel_astra_2002)
print('Owner: {}'.format(opel_astra_2002.owner))

Script prints:

Car(model='Opel Astra', year=2002)
Owner: Elon Musk

@pynista pynista changed the title Prettyprinter does not print dataclass default values Prettyprinter does not print dataclass optional default values Mar 3, 2020
@pynista pynista changed the title Prettyprinter does not print dataclass optional default values Prettyprinter does not print dataclass default values Mar 3, 2020
@gsauthof
Copy link

I can see that it can be advantageous for some use-cases to have default-valued fields excluded from the pretty-print.

But it would also be great to be able to configure this aspect of the dataclasses pretty-printing.

Relevant code where fields with default values are excluded:

display_attr = False
if (
field_def.default is MISSING and
field_def.default_factory is MISSING
):
display_attr = True
elif field_def.default is not MISSING:
if field_def.default != getattr(value, field_def.name):
display_attr = True
elif field_def.default_factory is not MISSING:
default_value = field_def.default_factory()
if default_value != getattr(value, field_def.name):
display_attr = True
else:
assert "default and default_factory should not be both defined"
if display_attr:
kwargs.append((field_def.name, getattr(value, field_def.name)))

@sigprof
Copy link

sigprof commented Aug 7, 2024

The same thing also happens with attrs classes.

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

No branches or pull requests

3 participants