-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add private attributes support (#1679)
* Add private attributes support * Add more blank lines in example * Add changes file * Update docs/usage/models.md Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * fix after bad merge * Add PrivateAttr, Config.underscore_attrs_are_private * remove unrelated change in utils.py * add # noqa: C901 (ignore complexity) to __setattr__ (see comment in PR) * add annotation to Config.underscore_attrs_are_private Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * use sunder names * mention underscore_attrs_are_private in model_config.md * add comment about default factory * fix comment * fix comment * clarify that both dunder and sunder names might be used * tweak docs and name * _set_default_private_attributes -> _init_private_attributes Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * use new name _init_private_attributes * move tests * copy private attributes in BaseModel.copy() * add test for default and default_factory used together * fix linting * more tests, default_factory kw only Co-authored-by: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
- Loading branch information
1 parent
2f7e404
commit 664cbcf
Showing
13 changed files
with
348 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add private attributes support |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from datetime import datetime | ||
from random import randint | ||
|
||
from pydantic import BaseModel, PrivateAttr | ||
|
||
|
||
class TimeAwareModel(BaseModel): | ||
_processed_at: datetime = PrivateAttr(default_factory=datetime.now) | ||
_secret_value: str = PrivateAttr() | ||
|
||
def __init__(self, **data): | ||
super().__init__(**data) | ||
# this could also be done with default_factory | ||
self._secret_value = randint(1, 5) | ||
|
||
|
||
m = TimeAwareModel() | ||
print(m._processed_at) | ||
print(m._secret_value) |
16 changes: 16 additions & 0 deletions
16
docs/examples/private_attributes_underscore_attrs_are_private.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import ClassVar | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Model(BaseModel): | ||
_class_var: ClassVar[str] = 'class var value' | ||
_private_attr: str = 'private attr value' | ||
|
||
class Config: | ||
underscore_attrs_are_private = True | ||
|
||
|
||
print(Model._class_var) | ||
print(Model._private_attr) | ||
print(Model()._private_attr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.