|
2 | 2 |
|
3 | 3 | Provides easy way to map dict to/from Full-Fledged 'JsonModel' object. |
4 | 4 |
|
5 | | - |
| 5 | + |
6 | 6 |  |
7 | 7 |
|
8 | 8 | ## Documentation |
9 | 9 |
|
10 | | -**[📄 Detailed Documentation](https://xyngular.github.io/py-xmodel/latest/)** | **[🐍 PyPi](https://pypi.org/project/xmodel/)** |
11 | | - |
12 | | -## Getting Started |
13 | | - |
14 | | -???+ warning "Alpha Software!" |
15 | | - This is pre-release Alpha software, based on another code base and |
16 | | - the needed changes to make a final release version are not yet |
17 | | - completed. Everything is subject to change! |
| 10 | +**[📄 Detailed Documentation](https://joshorr.github.io/pydantic-partials/latest/)** | **[🐍 PyPi](https://pypi.org/project/pydantic-partials/)** |
18 | 11 |
|
| 12 | +## Quick Start |
19 | 13 |
|
20 | 14 | ```shell |
21 | | -poetry install xmodel |
| 15 | +poetry install pydantic-partials |
22 | 16 | ``` |
23 | 17 |
|
24 | 18 | or |
25 | 19 |
|
26 | 20 | ```shell |
27 | | -pip install xmodel |
| 21 | +pip install pydantic-partials |
28 | 22 | ``` |
29 | 23 |
|
30 | | -Very basic example: |
| 24 | +By default, all fields without a default value will have the ability to be partial, |
| 25 | +and can be missing from both validation and serialization. |
| 26 | + |
| 27 | +Very basic example is below: |
31 | 28 |
|
32 | 29 | ```python |
33 | | -from xmodel import JsonModel |
| 30 | +from pydantic_partials import PartialModel, Missing, Partial |
34 | 31 |
|
35 | | -class MyModel(JsonModel): |
| 32 | + |
| 33 | +class MyModel(PartialModel): |
36 | 34 | some_attr: str |
37 | 35 |
|
38 | | -json_dict_input = {'some_attr': 'a-value'} |
39 | 36 |
|
40 | | -obj = MyModel(json_dict_input) |
41 | | -assert obj.some_attr == 'a-value' |
| 37 | +# By default, Partial fields without any value will get set to a special `Missing` type. |
| 38 | +# Any field that is set to Missing is excluded from the model_dump/model_dump_json |
| 39 | +obj = MyModel() |
| 40 | +assert obj.some_attr is Missing |
| 41 | +assert obj.model_dump() == {} |
| 42 | + |
| 43 | +# You can set the real value at any time, and it will behave like expected. |
| 44 | +obj.some_attr = 'hello' |
| 45 | +assert obj.some_attr is 'hello' |
| 46 | +assert obj.model_dump() == {'some_attr': 'hello'} |
| 47 | + |
| 48 | +# You can always manually set a field to `Missing` directly. |
| 49 | +obj.some_attr = Missing |
| 50 | + |
| 51 | +# And now it's removed from the model-dump. |
| 52 | +assert obj.model_dump() == {} |
| 53 | + |
| 54 | +# The json dump is also affected in the same way. |
| 55 | +assert obj.model_dump_json() == '{}' |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +You can turn off this default behavior by via `auto_partials` class argument or modeL_config option: |
| 60 | + |
| 61 | +```python |
| 62 | +from pydantic_partials import PartialModel, PartialConfigDict |
| 63 | + |
| 64 | +class TestModel1(PartialModel, auto_partials=False): |
| 65 | + ... |
| 66 | + |
| 67 | +class TestModel2(PartialModel): |
| 68 | + model_config = PartialConfigDict(auto_partials=False) |
| 69 | + ... |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +You can disable this automatic function. This means you have complete control of exactly which field |
| 74 | +can be partial or not. You can use either the generic `Partial[...]` generic or a union with `MissingType` |
| 75 | +to mark a field as a partial field. The generic simple makes the union to MissingType for you. |
| 76 | + |
| 77 | +Example of disabling auto_partials: |
| 78 | + |
| 79 | +```python |
| 80 | +from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict |
| 81 | +from decimal import Decimal |
| 82 | +from pydantic import ValidationError |
| 83 | + |
| 84 | +class TestModel(PartialModel, auto_partials=False): |
| 85 | + # Can use `Partial` generic type |
| 86 | + partial_int: Partial[int] = Missing |
| 87 | + |
| 88 | + # Or union with `MissingType` |
| 89 | + partial_str: str | MissingType |
| 90 | + |
| 91 | + required_decimal: Decimal |
| 92 | + |
| 93 | +try: |
| 94 | + TestModel() |
| 95 | +except ValidationError as e: |
| 96 | + print(f'Pydantic will state `required_decimal` is required: {e}') |
| 97 | +else: |
| 98 | + raise Exception('Pydantic should have required `required_decimal`.') |
| 99 | + |
| 100 | +obj = TestModel(required_decimal='1.34') |
| 101 | + |
| 102 | +# You can find out at any time if a field is missing or not: |
| 103 | +assert obj.partial_int is Missing |
| 104 | +assert obj.partial_str is Missing |
| 105 | + |
| 106 | +assert obj.required_decimal == Decimal('1.34') |
42 | 107 |
|
43 | | -json_dict = obj.api.json() |
44 | | -assert json_dict == json_dict_input |
45 | 108 | ``` |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments