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
add exclude_private parameter #1139
Conversation
This is failing, but even if it was passing I'm not sure I agree with it. It adds even more logic and arguments to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon reflection, I'm prepared to accept this.
Please:
- make the changes as requested
- add tests
- add a change description
- add a section to the docs (I guess in
models.md
) documenting this feature
@@ -277,7 +277,7 @@ def __init__(__pydantic_self__, **data: Any) -> None: | |||
|
|||
@no_type_check | |||
def __setattr__(self, name, value): | |||
if self.__config__.extra is not Extra.allow and name not in self.__fields__: | |||
if self.__config__.extra is Extra.forbid and name not in self.__fields__: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.__config__.extra is Extra.forbid and name not in self.__fields__: | |
if name.startswith('_'): | |
self.__dict__[name] = value | |
return | |
if self.__config__.extra is not Extra.allow and name not in self.__fields__: |
@@ -307,6 +307,7 @@ def dict( | |||
exclude_unset: bool = False, | |||
exclude_defaults: bool = False, | |||
exclude_none: bool = False, | |||
exclude_private: bool = True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this, I think it's safe to always exclude anything in __dict__
where the key startswith with _
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this would break concept when we have only parsed values in __dict__
and will force additional checks, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from pydantic import BaseModel, Extra
class Record(BaseModel):
class Config:
extra = Extra.allow
print(Record(_a='b')) # _a='b'
Personally, I think that the best solution for this problem, when you want to set your own private attrs which must not be included in fields is to use Maybe it should be documented as a standard way of doing this to avoid confusion in future. I don't see any disadvantages of |
I agree with @MrMrRobat and also see the performance concerns, let's discuss other solutions on #655. |
Maybe a disadvantage is, that it does not really work?
|
No, not good enough. Please reword your comments to be more polite. You might also think about crafting a more articulate description of your point here - I'm not really sure what you point is. |
Rereading the comment mentioning slots, it also adds But that does not really address private variables to be used like normal members, which was the point of this PR right? At least for us, the slots approach does not really solve our problem. This PR would have been a great step into this direction. |
@maxnoe, maybe it's an option to convert the Pydantic model to a Box tree (see this project https://github.com/cdgriffith/Box) after the initial setup? But in general I agree with you, my use case is a slight modification of data send to an API endpoint. What I currently do is create a second version of my whole model that inherits from the base model and only includes the additional fields. Then I use |
Change Summary
Add
exclude_private
parameter toBaseModel.dict
andBaseModel.json
, allowBaseModel.__setattr__
to register properties ifextra == Extra.ignore
.This PR would solve the problem described in the issue linked below: User can add private properties at runtime and exclude them from export.
Related issue number
#655
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)