Skip to content

Commit 7a680c3

Browse files
committed
feat: final code adjustments for the initial release of pydantic-partials.
1 parent d0e9aa8 commit 7a680c3

File tree

20 files changed

+788
-813
lines changed

20 files changed

+788
-813
lines changed

.github/workflows/publish-docs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
on: workflow_dispatch
2+
name: Publish Docs
3+
jobs:
4+
reusable:
5+
uses: joshorr/workflows/.github/workflows/py-publish-docs.yml@main
6+
secrets: inherit

.github/workflows/push-main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
on:
2+
push:
3+
branches:
4+
- main
5+
name: Push Main
6+
jobs:
7+
reusable:
8+
uses: joshorr/workflows/.github/workflows/py-push-main.yml@main
9+
with:
10+
real-publish: true
11+
secrets: inherit

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: test-local
2+
on: pull_request
3+
jobs:
4+
reusable:
5+
uses: joshorr/workflows/.github/workflows/py-test.yml@main
6+
secrets: inherit

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Custom Ones
2+
3+
/scripts
4+
5+
16
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
27
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
38

CHANGELOG.md

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,2 @@
11
# Changelog
22

3-
## [0.8.0](https://github.com/xyngular/py-xmodel/compare/v0.7.0...v0.8.0) (2023-09-27)
4-
5-
6-
### Features
7-
8-
* number of features to support xdynamo. ([2abd1d7](https://github.com/xyngular/py-xmodel/commit/2abd1d7a103e205b50ada9b1db54b52bdda9952b))
9-
10-
## [0.7.0](https://github.com/xyngular/py-xmodel/compare/v0.6.0...v0.7.0) (2023-09-15)
11-
12-
13-
### Features
14-
15-
* remove unneeded dependency. ([ab67b1d](https://github.com/xyngular/py-xmodel/commit/ab67b1d2d63dbb95c18ae39161cb589b243a9fde))
16-
17-
## [0.6.0](https://github.com/xyngular/py-xmodel/compare/v0.5.0...v0.6.0) (2023-05-19)
18-
19-
20-
### Features
21-
22-
* allow json-strarg as first positional argument for BaseModel. ([4278518](https://github.com/xyngular/py-xmodel/commit/42785180a7a56c8e3e6e0bed7d3eb4b1618586a4))
23-
24-
## [0.5.0](https://github.com/xyngular/py-xmodel/compare/v0.4.1...v0.5.0) (2023-05-05)
25-
26-
27-
### Features
28-
29-
* lazily copy any default-BaseModel instance when needed. ([f26bfd0](https://github.com/xyngular/py-xmodel/commit/f26bfd0f17ab103b9889199deffb341be8a5ee72))
30-
31-
## [0.4.1](https://github.com/xyngular/py-xmodel/compare/v0.4.0...v0.4.1) (2023-05-04)
32-
33-
34-
### Bug Fixes
35-
36-
* Ensure model/instance-ties BaseApi has default-converters from Class-tied BaseApi object. ([d77aecd](https://github.com/xyngular/py-xmodel/commit/d77aecd21e684e0a68ae96127e444ea3c1158100))
37-
38-
## [0.4.0](https://github.com/xyngular/py-xmodel/compare/v0.3.1...v0.4.0) (2023-05-02)
39-
40-
41-
### Features
42-
43-
* support `Self` and `dataclass-transforms` from Python 3.11 (python < 3.11 still works). ([f9dd9ef](https://github.com/xyngular/py-xmodel/commit/f9dd9ef29e8baa725a152931acb4008695245165))
44-
45-
## [0.3.1](https://github.com/xyngular/py-xmodel/compare/v0.3.0...v0.3.1) (2023-04-15)
46-
47-
48-
### Bug Fixes
49-
50-
* license ([c1fea47](https://github.com/xyngular/py-xmodel/commit/c1fea478660e5d8e88a7c446640db44fe8942615))
51-
52-
53-
### Documentation
54-
55-
* add doc links. ([2beff5c](https://github.com/xyngular/py-xmodel/commit/2beff5c7a2cdf2cf3d133edaf9b6bc32ba7e3e7d))
56-
57-
## [0.3.0](https://github.com/xyngular/py-xmodel/compare/v0.2.0...v0.3.0) (2023-04-15)
58-
59-
60-
### Features
61-
62-
* fix doc title. ([7df4e84](https://github.com/xyngular/py-xmodel/commit/7df4e844e975ac4c1bbce47edf4a5d0919aa8c42))
63-
64-
65-
### Bug Fixes
66-
67-
* don't iterate dicts. ([4ca409d](https://github.com/xyngular/py-xmodel/commit/4ca409d3d211e6ebfbb93510f2628fecc518e443))
68-
69-
## [0.2.0](https://github.com/xyngular/py-xmodel/compare/v0.1.1...v0.2.0) (2023-04-13)
70-
71-
72-
### Features
73-
74-
* allow later versions of python. ([5e93b10](https://github.com/xyngular/py-xmodel/commit/5e93b1036df794b2333ef8b0bc50bfa49188e023))
75-
76-
## [0.1.1](https://github.com/xyngular/py-xmodel/compare/v0.1.0...v0.1.1) (2023-04-08)
77-
78-
79-
### Bug Fixes
80-
81-
* doc generator. ([f6f9e0c](https://github.com/xyngular/py-xmodel/commit/f6f9e0ce289faf4b59608de68d9da64fb14618bd))
82-
83-
## 0.1.0 (2023-04-08)
84-
85-
86-
### Features
87-
88-
* add tomlkit to dev deps. ([0226c6e](https://github.com/xyngular/py-xmodel/commit/0226c6e50e5ace30c7480517b965cf8b7f429268))
89-
* add workflows ([c4810eb](https://github.com/xyngular/py-xmodel/commit/c4810eba9380ef5ca63be1aaaca2c0c5daa343bd))
90-
* fix refrences ([9259f18](https://github.com/xyngular/py-xmodel/commit/9259f18875eefd3ae88c4d4c6139848636425feb))
91-
* initial imported code ([9981452](https://github.com/xyngular/py-xmodel/commit/9981452059efe8f44f6c7bf99571424771976167))
92-
* rename error classes ([8764e0b](https://github.com/xyngular/py-xmodel/commit/8764e0bfa2719c5224a99e2f8c203c04ab5d5b43))
93-
94-
95-
### Bug Fixes
96-
97-
* various refs fixed up. ([7746cb6](https://github.com/xyngular/py-xmodel/commit/7746cb6aa5ae57832cea3031151a4bd0dfddbd0f))

docs/index.md

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ title: Getting Started
44
## Getting Started
55

66
```shell
7-
poetry install pydantic-lazy
7+
poetry install pydantic-partials
88
```
99

1010
or
1111

1212
```shell
13-
pip install pydantic-lazy
13+
pip install pydantic-partials
1414
```
1515

16-
Very basic example:
16+
By default, all fields without a default value will have the ability to be partial,
17+
and can be missing from both validation and serialization.
18+
19+
Very basic example is below:
1720

1821
```python
19-
from pydantic_lazy import LazyModel, Missing, Partial
22+
from pydantic_partials import PartialModel, Missing, Partial
23+
24+
25+
class MyModel(PartialModel):
26+
some_attr: str
2027

21-
class MyModel(LazyModel):
22-
some_attr: Partial[str]
2328

2429
# By default, Partial fields without any value will get set to a special `Missing` type.
2530
# Any field that is set to Missing is excluded from the model_dump/model_dump_json
@@ -34,29 +39,66 @@ assert obj.model_dump() == {'some_attr': 'hello'}
3439

3540
# You can always manually set a field to `Missing` directly.
3641
obj.some_attr = Missing
42+
43+
# And now it's removed from the model-dump.
3744
assert obj.model_dump() == {}
45+
46+
# The json dump is also affected in the same way.
47+
assert obj.model_dump_json() == '{}'
48+
```
49+
50+
51+
You can turn off this default behavior by via `auto_partials` class argument or modeL_config option:
52+
53+
```python
54+
from pydantic_partials import PartialModel, PartialConfigDict
55+
56+
class TestModel1(PartialModel, auto_partials=False):
57+
...
58+
59+
class TestModel2(PartialModel):
60+
model_config = PartialConfigDict(auto_partials=False)
61+
...
62+
3863
```
3964

65+
You can disable this automatic function. This means you have complete control of exactly which field
66+
can be partial or not. You can use either the generic `Partial[...]` generic or a union with `MissingType`
67+
to mark a field as a partial field. The generic simple makes the union to MissingType for you.
4068

41-
## Lazily Fetch Fields
69+
Example of disabling auto_partials:
4270

4371
```python
44-
from typing import Any
45-
from pydantic_lazy import LazyModel, Missing, Partial
46-
47-
class MyModel(LazyModel):
48-
def model_resolve_missing(self, field_name: str) -> Any:
49-
# ... Lookup the `field_name` and return its value
50-
if field_name == 'some_attr':
51-
return "fetched"
52-
# You can also return Missing to indicate that it's still missing/unfetchable.
53-
return Missing
54-
some_attr: Partial[str]
55-
another_attr: Partial[int]
56-
57-
# Resolve function called whenever a Missing value would otherwise be returned.
58-
assert MyModel().some_attr == 'fetched'
59-
assert MyModel().another_attr is Missing
60-
assert MyModel(some_attr='other-value').some_attr == 'other-value'
72+
from pydantic_partials import PartialModel, Missing, MissingType, Partial, PartialConfigDict
73+
from decimal import Decimal
74+
from pydantic import ValidationError
75+
76+
class TestModel(PartialModel, auto_partials=False):
77+
# Can use `Partial` generic type
78+
partial_int: Partial[int] = Missing
79+
80+
# Or union with `MissingType`
81+
partial_str: str | MissingType
82+
83+
required_decimal: Decimal
84+
85+
try:
86+
TestModel()
87+
except ValidationError as e:
88+
print(f'Pydantic will state `required_decimal` is required: {e}')
89+
else:
90+
raise Exception('Pydantic should have required `required_decimal`.')
91+
92+
obj = TestModel(required_decimal='1.34')
93+
94+
# You can find out at any time if a field is missing or not:
95+
assert obj.partial_int is Missing
96+
assert obj.partial_str is Missing
97+
98+
assert obj.required_decimal == Decimal('1.34')
6199

62100
```
101+
102+
103+
104+

mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# yaml-language-server: $schema=https://squidfunk.github.io/mkdocs-material/schema.json
22

3-
site_name: Docs for pydantic-lazy
3+
site_name: Docs for pydantic-partials
44
dev_addr: 127.0.0.1:7000
55

66
nav:
77
- xmodel:
88
- index.md
9-
- API Reference: api/xmodel/" target="_blank
9+
- API Reference: api/pydantic_partials/" target="_blank
1010
- Changelog: changelog.md
1111

1212
theme:

0 commit comments

Comments
 (0)