Skip to content

Commit a0eb544

Browse files
committed
docs: added basic docs to readme.
1 parent eb1eb7c commit a0eb544

File tree

1 file changed

+85
-19
lines changed

1 file changed

+85
-19
lines changed

README.md

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,110 @@
22

33
Provides easy way to map dict to/from Full-Fledged 'JsonModel' object.
44

5-
![PythonSupport](https://img.shields.io/static/v1?label=python&message=%203.8|%203.9|%203.10|%203.11&color=blue?style=flat-square&logo=python)
5+
![PythonSupport](https://img.shields.io/static/v1?label=python&message=%203.10|%203.11|%203.12&color=blue?style=flat-square&logo=python)
66
![PyPI version](https://badge.fury.io/py/xmodel.svg?)
77

88
## Documentation
99

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/)**
1811

12+
## Quick Start
1913

2014
```shell
21-
poetry install xmodel
15+
poetry install pydantic-partials
2216
```
2317

2418
or
2519

2620
```shell
27-
pip install xmodel
21+
pip install pydantic-partials
2822
```
2923

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:
3128

3229
```python
33-
from xmodel import JsonModel
30+
from pydantic_partials import PartialModel, Missing, Partial
3431

35-
class MyModel(JsonModel):
32+
33+
class MyModel(PartialModel):
3634
some_attr: str
3735

38-
json_dict_input = {'some_attr': 'a-value'}
3936

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')
42107

43-
json_dict = obj.api.json()
44-
assert json_dict == json_dict_input
45108
```
109+
110+
111+

0 commit comments

Comments
 (0)