Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exclude as field parameter (#2231)
* Add exclude/include as field parameters - Add "exclude" / "include" as a field parameter so that it can be configured using model config (or fields) instead of purely at `.dict` / `.json` export time. - Unify merging logic of advanced include/exclude fields - Add tests for merging logic and field/config exclude/include params - Closes #660 * Precompute include/exclude fields for class * Increase test coverage * Remove (now) redundant type checks in Model._iter: New exclusion/inclusion algorithms guarantee that no sets are passed further down. * Add docs for advanced field level exclude/include settings * Minimal optimization for simple exclude/include export Running benchmarks this vs. master is at: this: pydantic best=33.225μs/iter avg=33.940μs/iter stdev=1.120μs/iter version=1.7.3 master: pydantic best=32.901μs/iter avg=33.276μs/iter stdev=0.242μs/iter version=1.7.3 * Apply review comments on exclude/enclude field arguments * Fix/simplify type annotations * Allow both ``True`` and ``Ellipsis`` to be used to indicate full field exclusion * Reenable hypothesis plugin (removed by mistake) * Update advanced include/include docs to use ``True`` instead of ``...`` * Move field info exclude/include updates into FieldInfo class This way, the model field object does not need to concern itself with dealing with field into specific fields. (Same was done for alias in a previous commit). * remove double back tick in markdown. Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
- Loading branch information
1 parent
822be39
commit db697cc
Showing
14 changed files
with
609 additions
and
164 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 "exclude" as a field parameter so that it can be configured using model config instead of purely at `.dict` / `.json` export time. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pydantic import BaseModel, Field, SecretStr | ||
|
||
|
||
class User(BaseModel): | ||
id: int | ||
username: str | ||
password: SecretStr = Field(..., exclude=True) | ||
|
||
|
||
class Transaction(BaseModel): | ||
id: str | ||
user: User = Field(..., exclude={'username'}) | ||
value: int | ||
|
||
class Config: | ||
fields = {'value': {'exclude': True}} | ||
|
||
|
||
t = Transaction( | ||
id='1234567890', | ||
user=User( | ||
id=42, | ||
username='JohnDoe', | ||
password='hashedpassword' | ||
), | ||
value=9876543210, | ||
) | ||
|
||
print(t.dict()) |
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,26 @@ | ||
from pydantic import BaseModel, Field, SecretStr | ||
|
||
|
||
class User(BaseModel): | ||
id: int | ||
username: str # overridden by explicit exclude | ||
password: SecretStr = Field(exclude=True) | ||
|
||
|
||
class Transaction(BaseModel): | ||
id: str | ||
user: User | ||
value: int | ||
|
||
|
||
t = Transaction( | ||
id='1234567890', | ||
user=User( | ||
id=42, | ||
username='JohnDoe', | ||
password='hashedpassword' | ||
), | ||
value=9876543210, | ||
) | ||
|
||
print(t.dict(exclude={'value': True, 'user': {'username'}})) |
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,26 @@ | ||
from pydantic import BaseModel, Field, SecretStr | ||
|
||
|
||
class User(BaseModel): | ||
id: int = Field(..., include=True) | ||
username: str = Field(..., include=True) # overridden by explicit include | ||
password: SecretStr | ||
|
||
|
||
class Transaction(BaseModel): | ||
id: str | ||
user: User | ||
value: int | ||
|
||
|
||
t = Transaction( | ||
id='1234567890', | ||
user=User( | ||
id=42, | ||
username='JohnDoe', | ||
password='hashedpassword' | ||
), | ||
value=9876543210, | ||
) | ||
|
||
print(t.dict(include={'id': True, 'user': {'id'}})) |
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.