Skip to content

Commit 50e404e

Browse files
authored
Merge pull request #11 from coherent-oss/feature/GH-10-add-pydantic-support
GH-10: Adding Pydantic v2 support
2 parents a26b30c + 1c23ab0 commit 50e404e

File tree

19 files changed

+203
-15
lines changed

19 files changed

+203
-15
lines changed

NEWS.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
v0.5.1
2-
======
1+
0.5.1
2+
=====
33

44
Bugfixes
55
--------

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Build your Kubernetes manifests for Kustomize in Python!
2121

2222
* PyPI: https://pypi.org/project/kustomize/
23-
* Repository: https://github.com/yougov/python-kustomize
23+
* Repository: https://github.com/coherent-oss/python-kustomize
2424
* Documentation: https://python-kustomize.readthedocs.io/en/latest/
2525

2626
Overview
@@ -55,6 +55,7 @@ supports a number of object definition types:
5555

5656
* ``dict``
5757
* ``dataclasses``
58+
* ``BaseModel`` (from Pydantic)
5859
* ``attr`` (needs the library installed)
5960
* ``kubernetes`` (needs the library installed)
6061

kustomize/generators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def is_attr_class(obj) -> bool:
160160
return attr.has(type(obj))
161161

162162

163+
def is_pydantic_model(obj) -> bool:
164+
try:
165+
from pydantic import BaseModel
166+
except ImportError: # pragma: no cover
167+
return False
168+
169+
return isinstance(obj, BaseModel)
170+
171+
163172
def _is_kubernetes(obj):
164173
return (
165174
hasattr(obj, 'to_dict')
@@ -227,6 +236,9 @@ def to_dict_or_dicts(obj):
227236
import attr
228237

229238
obj = attr.asdict(obj, recurse=True)
239+
elif is_pydantic_model(obj):
240+
logger.debug('Object is a Pydantic model, using it for conversion')
241+
obj = obj.model_dump()
230242
elif hasattr(obj, '__dict__'):
231243
obj = obj.__dict__
232244

newsfragments/10.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adding Pydantic v2 support.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ testing = [
3737
"coverage",
3838
"PyYAML",
3939
"types-PyYAML",
40+
"attrs",
41+
"Pydantic >= 2.0",
4042
]
4143
docs = [
4244
# upstream

tests/fixtures/attrs/python/configMap.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import attr
1+
from pydantic import BaseModel
22

33

4-
@attr.s(auto_attribs=True)
5-
class Metadata:
4+
class Metadata(BaseModel):
65
name: str
76

87

9-
@attr.s(auto_attribs=True)
10-
class ConfigMap:
8+
class ConfigMap(BaseModel):
119
metadata: Metadata
1210
data: dict
1311
apiVersion: str = 'v1'

tests/fixtures/attrs/python/deployment.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
class CustomDeployment:
2-
def to_dict(self) -> dict:
1+
from pydantic import BaseModel
2+
3+
4+
class CustomDeployment(BaseModel):
5+
def model_dump(self) -> dict:
36
return {
47
'apiVersion': 'apps/v1',
58
'kind': 'Deployment',

tests/fixtures/attrs/python/kustomization.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
from dataclasses import dataclass
21
from typing import List
32

3+
from pydantic import BaseModel
44

5-
@dataclass
6-
class CommonLabels:
5+
6+
class CommonLabels(BaseModel):
77
app: str
88

99

10-
@dataclass
11-
class MyKustomization:
10+
class MyKustomization(BaseModel):
1211
commonLabels: CommonLabels
1312
resources: List[str]
1413

tests/fixtures/pydantic_v2/__init__.py

Whitespace-only changes.

tests/fixtures/pydantic_v2/python/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)