Skip to content

Commit d09b8a0

Browse files
committed
docs: added clarification around inheritance and default values.
1 parent fe6779b commit d09b8a0

File tree

3 files changed

+101
-11
lines changed

3 files changed

+101
-11
lines changed

README.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ and can be missing from both validation and serialization.
2727
Very basic example is below:
2828

2929
```python
30-
from pydantic_partials import PartialModel, Missing, Partial
30+
from pydantic_partials import PartialModel, Missing
3131

3232

3333
class MyModel(PartialModel):
@@ -61,7 +61,6 @@ obj.another_field = 'assigned-value'
6161
assert obj.model_dump() == {'another_field': 'assigned-value'}
6262
```
6363

64-
6564
You can turn off this default behavior by via `auto_partials` class argument or modeL_config option:
6665

6766
```python
@@ -73,7 +72,6 @@ class TestModel1(PartialModel, auto_partials=False):
7372
class TestModel2(PartialModel):
7473
model_config = PartialConfigDict(auto_partials=False)
7574
...
76-
7775
```
7876

7977
You can disable this automatic function. This means you have complete control of exactly which field
@@ -83,7 +81,7 @@ to mark a field as a partial field. The generic simple makes the union to Missi
8381
Example of disabling auto_partials:
8482

8583
```python
86-
from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict
84+
from pydantic_partials import PartialModel, Missing, MissingType, Partial
8785
from decimal import Decimal
8886
from pydantic import ValidationError
8987

@@ -110,8 +108,43 @@ assert obj.partial_int is Missing
110108
assert obj.partial_str is Missing
111109

112110
assert obj.required_decimal == Decimal('1.34')
113-
114111
```
115112

116113

117114

115+
You can inherit from a model to make a partial-version of the inherited fields:
116+
117+
```python
118+
from pydantic_partials import PartialModel, Missing
119+
from pydantic import ValidationError, BaseModel
120+
121+
122+
class TestModel(BaseModel):
123+
name: str
124+
value: str
125+
some_null_by_default_field: str | None = None
126+
127+
128+
try:
129+
TestModel()
130+
except ValidationError as e:
131+
print(f'Pydantic will state `name` + `value` are required: {e}')
132+
else:
133+
raise Exception('Pydantic should have required `required_decimal`.')
134+
135+
136+
class PartialTestModel(PartialModel, TestModel):
137+
pass
138+
139+
140+
obj = PartialTestModel(name='a-name')
141+
142+
assert obj.name == 'a-name'
143+
assert obj.value is Missing
144+
assert obj.some_null_by_default_field is None
145+
```
146+
147+
Notice that if a field has a default value, it's used instead of marking it as `Missing`.
148+
149+
Also, the `Missing` sentinel value is a separate value vs `None`, allowing one to easily
150+
know if a value is truly just missing or is `None`/`Null`.

docs/index.md

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and can be missing from both validation and serialization.
1919
Very basic example is below:
2020

2121
```python
22-
from pydantic_partials import PartialModel, Missing, Partial
22+
from pydantic_partials import PartialModel, Missing
2323

2424

2525
class MyModel(PartialModel):
@@ -51,10 +51,8 @@ obj.another_field = 'assigned-value'
5151

5252
# And now it's removed from the model-dump.
5353
assert obj.model_dump() == {'another_field': 'assigned-value'}
54-
5554
```
5655

57-
5856
You can turn off this default behavior by via `auto_partials` class argument or modeL_config option:
5957

6058
```python
@@ -66,7 +64,6 @@ class TestModel1(PartialModel, auto_partials=False):
6664
class TestModel2(PartialModel):
6765
model_config = PartialConfigDict(auto_partials=False)
6866
...
69-
7067
```
7168

7269
You can disable this automatic function. This means you have complete control of exactly which field
@@ -76,7 +73,7 @@ to mark a field as a partial field. The generic simple makes the union to Missi
7673
Example of disabling auto_partials:
7774

7875
```python
79-
from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict
76+
from pydantic_partials import PartialModel, Missing, MissingType, Partial
8077
from decimal import Decimal
8178
from pydantic import ValidationError
8279

@@ -103,9 +100,43 @@ assert obj.partial_int is Missing
103100
assert obj.partial_str is Missing
104101

105102
assert obj.required_decimal == Decimal('1.34')
106-
107103
```
108104

109105

110106

107+
You can inherit from a model to make a partial-version of the inherited fields:
108+
109+
```python
110+
from pydantic_partials import PartialModel, Missing
111+
from pydantic import ValidationError, BaseModel
112+
113+
114+
class TestModel(BaseModel):
115+
name: str
116+
value: str
117+
some_null_by_default_field: str | None = None
118+
119+
120+
try:
121+
TestModel()
122+
except ValidationError as e:
123+
print(f'Pydantic will state `name` + `value` are required: {e}')
124+
else:
125+
raise Exception('Pydantic should have required `required_decimal`.')
126+
127+
128+
class PartialTestModel(PartialModel, TestModel):
129+
pass
130+
131+
132+
obj = PartialTestModel(name='a-name')
133+
134+
assert obj.name == 'a-name'
135+
assert obj.value is Missing
136+
assert obj.some_null_by_default_field is None
137+
```
138+
139+
Notice that if a field has a default value, it's used instead of marking it as `Missing`.
111140

141+
Also, the `Missing` sentinel value is a separate value vs `None`, allowing one to easily
142+
know if a value is truly just missing or is `None`/`Null`.

tests/test_doc_examples.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,29 @@ class TestModel(PartialModel, auto_partials=False):
5656
assert obj.partial_int is Missing
5757
assert obj.partial_str is Missing
5858
assert obj.required_decimal == Decimal('1.34')
59+
60+
61+
def test_doc_example__index__3():
62+
from pydantic_partials import PartialModel, Missing
63+
from pydantic import ValidationError, BaseModel
64+
65+
class TestModel(BaseModel):
66+
name: str
67+
value: str
68+
some_null_by_default_field: str | None = None
69+
70+
try:
71+
TestModel()
72+
except ValidationError as e:
73+
print(f'Pydantic will state `name` + `value` are required: {e}')
74+
else:
75+
raise Exception('Pydantic should have required `required_decimal`.')
76+
77+
class PartialTestModel(PartialModel, TestModel):
78+
pass
79+
80+
obj = PartialTestModel(name='a-name')
81+
82+
assert obj.name == 'a-name'
83+
assert obj.value is Missing
84+
assert obj.some_null_by_default_field is None

0 commit comments

Comments
 (0)