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

repr behaviour dictated in ConfigDict #9415

Open
3 of 13 tasks
pygaiwan opened this issue May 8, 2024 · 1 comment
Open
3 of 13 tasks

repr behaviour dictated in ConfigDict #9415

pygaiwan opened this issue May 8, 2024 · 1 comment

Comments

@pygaiwan
Copy link

pygaiwan commented May 8, 2024

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

My usecase:
I have multiple Models with hundreds of fields each, I would like to have a repr of the Models with only some of the fields without having to override the __repr__ created by Pydantic. I could indeed set each field to be Field(repr=False) and keep the default repr=True to those that I need, but this approach sounds very labour intensive and counter productive.

Instead I think the Model should have a switch in ConfigDict to set the default of the repr field of each Field.

So in pydantic/fields.py instead of having

self.repr = kwargs.pop('repr', True)

You would have something like:

self.repr = kwargs.pop('repr', <ConfigDict repr value>)

With this approach then you will have:

from pydantic import BaseModel, ConfigDict, Field

class MyModel(BaseModel):
    model_config = ConfigDict(repr_default=False)

    x: int = Field(repr=True)
    y: int

And only x will be in the model. If no Field is configured with repr=True then it can either be an empty repr like:

>>> print(repr(MyModel(x=2, y=3)))
MyModel()

or the standard python repr like:

>>> print(repr(MyModel(x=2, y=3)))
<__main__.MyModel object at 0x7f52d12f2a50>

This feature I think would significantly simplify the management of model representations, especially for large models with numerous fields.

Affected Components

@sydney-runkle
Copy link
Member

@pygaiwan,

Thanks for the detailed feature request. I do have a general concern about the increased bloating of the config settings, and I don't feel as though this one is super high priority.

That being said, if you're interested in submitting a PR adding support for this, we can certainly consider it!

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

No branches or pull requests

2 participants