pydantic-addons 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydantic_addons-1.0.0/.github/workflows/ci.yml +28 -0
- pydantic_addons-1.0.0/.github/workflows/release.yml +20 -0
- pydantic_addons-1.0.0/.gitignore +168 -0
- pydantic_addons-1.0.0/.pre-commit-config.yaml +17 -0
- pydantic_addons-1.0.0/LICENSE +21 -0
- pydantic_addons-1.0.0/PKG-INFO +149 -0
- pydantic_addons-1.0.0/README.md +111 -0
- pydantic_addons-1.0.0/pyproject.toml +64 -0
- pydantic_addons-1.0.0/setup.cfg +4 -0
- pydantic_addons-1.0.0/src/pydantic_addons/__init__.py +21 -0
- pydantic_addons-1.0.0/src/pydantic_addons/_version.py +24 -0
- pydantic_addons-1.0.0/src/pydantic_addons/discriminated_union.py +120 -0
- pydantic_addons-1.0.0/src/pydantic_addons/py.typed +0 -0
- pydantic_addons-1.0.0/src/pydantic_addons/shared.py +24 -0
- pydantic_addons-1.0.0/src/pydantic_addons/tags.py +54 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/PKG-INFO +149 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/SOURCES.txt +22 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/dependency_links.txt +1 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/scm_file_list.json +19 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/scm_version.json +8 -0
- pydantic_addons-1.0.0/src/pydantic_addons.egg-info/top_level.txt +1 -0
- pydantic_addons-1.0.0/tests/test_discriminated_union.py +252 -0
- pydantic_addons-1.0.0/tests/test_shared_and_tags.py +122 -0
- pydantic_addons-1.0.0/uv.lock +525 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: ci
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [master]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
strategy:
|
|
12
|
+
# Each OS and version runs to completion, though another fails
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
16
|
+
python-version: ["3.13", "3.14"]
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
with:
|
|
20
|
+
# setuptools-scm derives the version from the tags
|
|
21
|
+
fetch-depth: 0
|
|
22
|
+
- uses: astral-sh/setup-uv@v6
|
|
23
|
+
with:
|
|
24
|
+
python-version: ${{ matrix.python-version }}
|
|
25
|
+
- run: uv sync --locked
|
|
26
|
+
# ruff and pyright, as when committing
|
|
27
|
+
- run: uv run pre-commit run --all-files --show-diff-on-failure
|
|
28
|
+
- run: uv run pytest --cov
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment: pypi
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
with:
|
|
16
|
+
# setuptools-scm derives the version from the tags
|
|
17
|
+
fetch-depth: 0
|
|
18
|
+
- uses: astral-sh/setup-uv@v6
|
|
19
|
+
- run: uv build
|
|
20
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
110
|
+
.pdm.toml
|
|
111
|
+
.pdm-python
|
|
112
|
+
.pdm-build/
|
|
113
|
+
|
|
114
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
115
|
+
__pypackages__/
|
|
116
|
+
|
|
117
|
+
# Celery stuff
|
|
118
|
+
celerybeat-schedule
|
|
119
|
+
celerybeat.pid
|
|
120
|
+
|
|
121
|
+
# SageMath parsed files
|
|
122
|
+
*.sage.py
|
|
123
|
+
|
|
124
|
+
# Environments
|
|
125
|
+
.env
|
|
126
|
+
.venv
|
|
127
|
+
env/
|
|
128
|
+
venv/
|
|
129
|
+
ENV/
|
|
130
|
+
env.bak/
|
|
131
|
+
venv.bak/
|
|
132
|
+
|
|
133
|
+
# Spyder project settings
|
|
134
|
+
.spyderproject
|
|
135
|
+
.spyproject
|
|
136
|
+
|
|
137
|
+
# Rope project settings
|
|
138
|
+
.ropeproject
|
|
139
|
+
|
|
140
|
+
# mkdocs documentation
|
|
141
|
+
/site
|
|
142
|
+
|
|
143
|
+
# mypy
|
|
144
|
+
.mypy_cache/
|
|
145
|
+
.dmypy.json
|
|
146
|
+
dmypy.json
|
|
147
|
+
|
|
148
|
+
# Pyre type checker
|
|
149
|
+
.pyre/
|
|
150
|
+
|
|
151
|
+
# pytype static type analyzer
|
|
152
|
+
.pytype/
|
|
153
|
+
|
|
154
|
+
# Cython debug symbols
|
|
155
|
+
cython_debug/
|
|
156
|
+
|
|
157
|
+
# PyCharm
|
|
158
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
159
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
160
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
161
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
162
|
+
.idea/
|
|
163
|
+
|
|
164
|
+
# VS Code: its settings hold paths of this machine
|
|
165
|
+
.vscode/
|
|
166
|
+
|
|
167
|
+
# setuptools-scm
|
|
168
|
+
src/pydantic_addons/_version.py
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
# Keep rev in line with ruff's version in uv.lock, so that both apply the same rules
|
|
3
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
4
|
+
rev: v0.17.5
|
|
5
|
+
hooks:
|
|
6
|
+
- id: ruff-check
|
|
7
|
+
args: [--fix]
|
|
8
|
+
- id: ruff-format
|
|
9
|
+
# The project's own pyright and environment, from uv.lock
|
|
10
|
+
- repo: local
|
|
11
|
+
hooks:
|
|
12
|
+
- id: pyright
|
|
13
|
+
name: pyright
|
|
14
|
+
entry: uv run pyright
|
|
15
|
+
language: system
|
|
16
|
+
types_or: [python, pyi]
|
|
17
|
+
pass_filenames: false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nick Young
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pydantic-addons
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Annotations which pydantic reads and writes, without depending on it: DiscriminatedUnion and Shared
|
|
5
|
+
Author-email: Nick Young <nick.young.github@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Nick Young
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
32
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Requires-Python: >=3.13
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
License-File: LICENSE
|
|
37
|
+
Dynamic: license-file
|
|
38
|
+
|
|
39
|
+
# pydantic-addons
|
|
40
|
+
|
|
41
|
+
Annotations for the fields of dataclasses and models, which [pydantic](https://docs.pydantic.dev) reads and
|
|
42
|
+
writes, and which do not depend on it. To a type checker each is the type it annotates. pydantic is imported only
|
|
43
|
+
when it asks an annotation how to read and write it, so a library may use them, and read them for itself, whether
|
|
44
|
+
or not pydantic is installed: as [dataclass-native](https://github.com/nickyoung-github/dataclass-native) does.
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
pip install pydantic-addons
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Python 3.13 or later.
|
|
51
|
+
|
|
52
|
+
## DiscriminatedUnion
|
|
53
|
+
|
|
54
|
+
A union whose value is written with a tag, which says which member it is of. So members which are written alike
|
|
55
|
+
are told apart when read: as two classes of the same fields are not, by a plain union.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from dataclasses import dataclass
|
|
59
|
+
|
|
60
|
+
from pydantic import TypeAdapter
|
|
61
|
+
from pydantic_addons import DiscriminatedUnion
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class Cash:
|
|
66
|
+
amount: float
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(frozen=True)
|
|
70
|
+
class Bond:
|
|
71
|
+
amount: float
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True)
|
|
75
|
+
class Trade:
|
|
76
|
+
instrument: DiscriminatedUnion[Cash, Bond]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
adapter = TypeAdapter(Trade)
|
|
80
|
+
adapter.dump_json(Trade(Bond(1.5)))
|
|
81
|
+
# b'{"instrument":{"t":"trades.Bond","v":{"amount":1.5}}}'
|
|
82
|
+
adapter.validate_json('{"instrument":{"t":"trades.Bond","v":{"amount":1.5}}}')
|
|
83
|
+
# Trade(instrument=Bond(amount=1.5))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- It is written as `{"t": tag, "v": value}`, and read from that with its entries in either order. In Python, a
|
|
87
|
+
value of a member is also validated as itself.
|
|
88
|
+
- A value is to be of a member exactly. A subclass of a member's is another type, with a tag of its own, so is
|
|
89
|
+
not written unless it is itself a member: nor is a `bool` as an `int`. An `int` is written as the `float` it
|
|
90
|
+
is, where it may not be an `int`.
|
|
91
|
+
- The members are to have tags of their own.
|
|
92
|
+
|
|
93
|
+
### Tags
|
|
94
|
+
|
|
95
|
+
| Type | Tag |
|
|
96
|
+
|---|---|
|
|
97
|
+
| a class given one by `@tagged` | that |
|
|
98
|
+
| any other dataclass, enum or pydantic model | its module and qualified name: `"trades.Bond"` |
|
|
99
|
+
| `Annotated[T, ...]` | the `__tag__` of the first of its metadata which has one: else the tag of `T` |
|
|
100
|
+
| a name given to a type, as `type Price = ...` | the tag of that type |
|
|
101
|
+
| anything else | its name, in lower case: `"int"`, `"datetime"`, `"uuid"` |
|
|
102
|
+
|
|
103
|
+
`tag(tp)` gives the tag of a type. Give a class a tag of its own so that it may be moved or renamed, and what
|
|
104
|
+
was written of it still be read:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from pydantic_addons import tagged
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@tagged("bond.v1")
|
|
111
|
+
@dataclass(frozen=True)
|
|
112
|
+
class Bond:
|
|
113
|
+
amount: float
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
A subclass does not have the tag its base was given: it is another type.
|
|
117
|
+
|
|
118
|
+
## Shared
|
|
119
|
+
|
|
120
|
+
`Shared[A]` says that the value is shared with whatever else holds it, not a copy of its own: `Shared[A]`,
|
|
121
|
+
`Shared[A | B]`, `Shared[A] | None`.
|
|
122
|
+
|
|
123
|
+
What that is, is for whatever reads the annotation to say. dataclass-native holds a shared object natively by a
|
|
124
|
+
pointer which counts its references, and writes it once for all that share it. To pydantic it is `A`: validated
|
|
125
|
+
and written as the value, wherever it is held.
|
|
126
|
+
|
|
127
|
+
## Reading the annotations
|
|
128
|
+
|
|
129
|
+
Each is `Annotated`, with a marker in its metadata: `DISCRIMINATED`, a `DiscriminatedMarker`, on the union of
|
|
130
|
+
the members, and `SHARED`, a `SharedMarker`, on the type.
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from typing import get_args
|
|
134
|
+
|
|
135
|
+
from pydantic_addons import DISCRIMINATED, SHARED, DiscriminatedUnion, Shared
|
|
136
|
+
|
|
137
|
+
get_args(DiscriminatedUnion[Cash, Bond]) # (Cash | Bond, DISCRIMINATED)
|
|
138
|
+
get_args(Shared[Cash]) # (Cash, SHARED)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Development
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
uv sync
|
|
145
|
+
uv run pre-commit install
|
|
146
|
+
uv run pytest
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
A release is a tag `v1.2.3`, which is its version: the release workflow builds it and publishes it to PyPI.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# pydantic-addons
|
|
2
|
+
|
|
3
|
+
Annotations for the fields of dataclasses and models, which [pydantic](https://docs.pydantic.dev) reads and
|
|
4
|
+
writes, and which do not depend on it. To a type checker each is the type it annotates. pydantic is imported only
|
|
5
|
+
when it asks an annotation how to read and write it, so a library may use them, and read them for itself, whether
|
|
6
|
+
or not pydantic is installed: as [dataclass-native](https://github.com/nickyoung-github/dataclass-native) does.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
pip install pydantic-addons
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Python 3.13 or later.
|
|
13
|
+
|
|
14
|
+
## DiscriminatedUnion
|
|
15
|
+
|
|
16
|
+
A union whose value is written with a tag, which says which member it is of. So members which are written alike
|
|
17
|
+
are told apart when read: as two classes of the same fields are not, by a plain union.
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
|
|
22
|
+
from pydantic import TypeAdapter
|
|
23
|
+
from pydantic_addons import DiscriminatedUnion
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class Cash:
|
|
28
|
+
amount: float
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class Bond:
|
|
33
|
+
amount: float
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class Trade:
|
|
38
|
+
instrument: DiscriminatedUnion[Cash, Bond]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
adapter = TypeAdapter(Trade)
|
|
42
|
+
adapter.dump_json(Trade(Bond(1.5)))
|
|
43
|
+
# b'{"instrument":{"t":"trades.Bond","v":{"amount":1.5}}}'
|
|
44
|
+
adapter.validate_json('{"instrument":{"t":"trades.Bond","v":{"amount":1.5}}}')
|
|
45
|
+
# Trade(instrument=Bond(amount=1.5))
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- It is written as `{"t": tag, "v": value}`, and read from that with its entries in either order. In Python, a
|
|
49
|
+
value of a member is also validated as itself.
|
|
50
|
+
- A value is to be of a member exactly. A subclass of a member's is another type, with a tag of its own, so is
|
|
51
|
+
not written unless it is itself a member: nor is a `bool` as an `int`. An `int` is written as the `float` it
|
|
52
|
+
is, where it may not be an `int`.
|
|
53
|
+
- The members are to have tags of their own.
|
|
54
|
+
|
|
55
|
+
### Tags
|
|
56
|
+
|
|
57
|
+
| Type | Tag |
|
|
58
|
+
|---|---|
|
|
59
|
+
| a class given one by `@tagged` | that |
|
|
60
|
+
| any other dataclass, enum or pydantic model | its module and qualified name: `"trades.Bond"` |
|
|
61
|
+
| `Annotated[T, ...]` | the `__tag__` of the first of its metadata which has one: else the tag of `T` |
|
|
62
|
+
| a name given to a type, as `type Price = ...` | the tag of that type |
|
|
63
|
+
| anything else | its name, in lower case: `"int"`, `"datetime"`, `"uuid"` |
|
|
64
|
+
|
|
65
|
+
`tag(tp)` gives the tag of a type. Give a class a tag of its own so that it may be moved or renamed, and what
|
|
66
|
+
was written of it still be read:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from pydantic_addons import tagged
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@tagged("bond.v1")
|
|
73
|
+
@dataclass(frozen=True)
|
|
74
|
+
class Bond:
|
|
75
|
+
amount: float
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
A subclass does not have the tag its base was given: it is another type.
|
|
79
|
+
|
|
80
|
+
## Shared
|
|
81
|
+
|
|
82
|
+
`Shared[A]` says that the value is shared with whatever else holds it, not a copy of its own: `Shared[A]`,
|
|
83
|
+
`Shared[A | B]`, `Shared[A] | None`.
|
|
84
|
+
|
|
85
|
+
What that is, is for whatever reads the annotation to say. dataclass-native holds a shared object natively by a
|
|
86
|
+
pointer which counts its references, and writes it once for all that share it. To pydantic it is `A`: validated
|
|
87
|
+
and written as the value, wherever it is held.
|
|
88
|
+
|
|
89
|
+
## Reading the annotations
|
|
90
|
+
|
|
91
|
+
Each is `Annotated`, with a marker in its metadata: `DISCRIMINATED`, a `DiscriminatedMarker`, on the union of
|
|
92
|
+
the members, and `SHARED`, a `SharedMarker`, on the type.
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from typing import get_args
|
|
96
|
+
|
|
97
|
+
from pydantic_addons import DISCRIMINATED, SHARED, DiscriminatedUnion, Shared
|
|
98
|
+
|
|
99
|
+
get_args(DiscriminatedUnion[Cash, Bond]) # (Cash | Bond, DISCRIMINATED)
|
|
100
|
+
get_args(Shared[Cash]) # (Cash, SHARED)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Development
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
uv sync
|
|
107
|
+
uv run pre-commit install
|
|
108
|
+
uv run pytest
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
A release is a tag `v1.2.3`, which is its version: the release workflow builds it and publishes it to PyPI.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantic-addons"
|
|
3
|
+
description = "Annotations which pydantic reads and writes, without depending on it: DiscriminatedUnion and Shared"
|
|
4
|
+
authors = [
|
|
5
|
+
{name = "Nick Young", email = "nick.young.github@gmail.com"}
|
|
6
|
+
]
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
license = {file = "LICENSE"}
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
|
|
11
|
+
classifiers = [
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
15
|
+
"Programming Language :: Python :: 3.13",
|
|
16
|
+
"Programming Language :: Python :: 3.14",
|
|
17
|
+
"Operating System :: OS Independent"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
dynamic = ["version"]
|
|
21
|
+
|
|
22
|
+
# None: pydantic is imported only when it asks an annotation how to read and write it
|
|
23
|
+
dependencies = []
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["setuptools>=64", "setuptools-scm>=8"]
|
|
27
|
+
build-backend = "setuptools.build_meta"
|
|
28
|
+
|
|
29
|
+
# The version is that of the latest tag, e.g. v1.2.3
|
|
30
|
+
[tool.setuptools_scm]
|
|
31
|
+
version_file = "src/pydantic_addons/_version.py"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
where = ["src"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.package-data]
|
|
37
|
+
pydantic_addons = ["py.typed"]
|
|
38
|
+
|
|
39
|
+
[dependency-groups]
|
|
40
|
+
dev = [
|
|
41
|
+
"pre-commit>=4.7.0",
|
|
42
|
+
"pydantic>=2.14.4",
|
|
43
|
+
"pyright>=1.1.414",
|
|
44
|
+
"pytest",
|
|
45
|
+
"pytest-cov",
|
|
46
|
+
"ruff>=0.17.5",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.pyright]
|
|
50
|
+
include = ["src", "tests"]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
testpaths = ["tests"]
|
|
54
|
+
|
|
55
|
+
[tool.coverage.run]
|
|
56
|
+
source = ["pydantic_addons"]
|
|
57
|
+
branch = true
|
|
58
|
+
# One test is of a process of its own, in which pydantic is not imported
|
|
59
|
+
patch = ["subprocess"]
|
|
60
|
+
omit = ["*/_version.py"]
|
|
61
|
+
|
|
62
|
+
[tool.coverage.report]
|
|
63
|
+
show_missing = true
|
|
64
|
+
skip_covered = true
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Annotations which pydantic reads and writes, without depending on it: to a type checker each is the type it
|
|
3
|
+
annotates, and pydantic is imported only when it asks one how to read and write it.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ._version import __version__
|
|
7
|
+
from .discriminated_union import DISCRIMINATED, DiscriminatedMarker, DiscriminatedUnion
|
|
8
|
+
from .shared import SHARED, Shared, SharedMarker
|
|
9
|
+
from .tags import tag, tagged
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"DISCRIMINATED",
|
|
13
|
+
"SHARED",
|
|
14
|
+
"DiscriminatedMarker",
|
|
15
|
+
"DiscriminatedUnion",
|
|
16
|
+
"Shared",
|
|
17
|
+
"SharedMarker",
|
|
18
|
+
"__version__",
|
|
19
|
+
"tag",
|
|
20
|
+
"tagged",
|
|
21
|
+
]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '1.0.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (1, 0, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = 'gf38045dde'
|