Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
Config.smart_union
option (#2092)
* feat: add `Config.smart_union` to prevent coercion in `Union` if possible * docs: write some documentation * Update docs/usage/model_config.md Thanks @djpugh Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com> * improve doc * support 3.10 * improve smart_union * Update docs/usage/types.md Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com> * put new sentence inside warning block * docs: reorder * rename is_union_origin into is_union * inverse and condition for perf * fix doc Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>
- Loading branch information
1 parent
eef4ac5
commit c38c463
Showing
13 changed files
with
285 additions
and
20 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 `Config.smart_union` to prevent coercion in `Union` if possible. See [the doc](https://pydantic-docs.helpmanual.io/usage/model_config/#smart-union) for more information |
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 typing import Union | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Foo(BaseModel): | ||
pass | ||
|
||
|
||
class Bar(BaseModel): | ||
pass | ||
|
||
|
||
class Model(BaseModel): | ||
x: Union[str, int] | ||
y: Union[Foo, Bar] | ||
|
||
|
||
print(Model(x=1, y=Bar())) |
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,22 @@ | ||
from typing import Union | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Foo(BaseModel): | ||
pass | ||
|
||
|
||
class Bar(BaseModel): | ||
pass | ||
|
||
|
||
class Model(BaseModel): | ||
x: Union[str, int] | ||
y: Union[Foo, Bar] | ||
|
||
class Config: | ||
smart_union = True | ||
|
||
|
||
print(Model(x=1, y=Bar())) |
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,14 @@ | ||
from typing import List, Union | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Model(BaseModel, smart_union=True): | ||
x: Union[List[str], List[int]] | ||
|
||
|
||
# Expected coercion | ||
print(Model(x=[1, '2'])) | ||
|
||
# Unexpected coercion | ||
print(Model(x=[1, 2])) |
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
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.