Skip to content

Commit f45ab97

Browse files
authored
Merge pull request #1561 from vitalik/pydantic-2.12-fix
pydantic 2.12 compatibility
2 parents 8f393df + ecb2759 commit f45ab97

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

.github/workflows/test_full.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
- name: Set up Python
5050
uses: actions/setup-python@v5
5151
with:
52-
python-version: '3.10'
52+
python-version: 3.12
5353
- name: Install Flit
5454
run: pip install flit "django>=5.2"
5555
- name: Install Dependencies
@@ -64,7 +64,7 @@ jobs:
6464
- name: Set up Python
6565
uses: actions/setup-python@v5
6666
with:
67-
python-version: 3.9
67+
python-version: 3.12
6868
- name: Install Flit
6969
run: pip install flit
7070
- name: Install Dependencies

ninja/params/models.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def resolve(
6565
return cls()
6666

6767
data = cls._map_data_paths(data)
68+
# Convert defaultdict to dict for pydantic 2.12+ compatibility
69+
# In pydantic 2.12+, accessing missing keys in defaultdict creates nested
70+
# defaultdicts which then fail validation
71+
if isinstance(data, defaultdict):
72+
data = dict(data)
6873
return cls.model_validate(data, context={"request": request})
6974

7075
@classmethod
@@ -197,7 +202,7 @@ def get_request_data(
197202
return results
198203

199204

200-
class Param(FieldInfo):
205+
class Param(FieldInfo): # type: ignore[misc]
201206
def __init__(
202207
self,
203208
default: Any,
@@ -260,35 +265,35 @@ def _param_source(cls) -> str:
260265
return cls.__name__.lower()
261266

262267

263-
class Path(Param):
268+
class Path(Param): # type: ignore[misc]
264269
_model = PathModel
265270

266271

267-
class Query(Param):
272+
class Query(Param): # type: ignore[misc]
268273
_model = QueryModel
269274

270275

271-
class Header(Param):
276+
class Header(Param): # type: ignore[misc]
272277
_model = HeaderModel
273278

274279

275-
class Cookie(Param):
280+
class Cookie(Param): # type: ignore[misc]
276281
_model = CookieModel
277282

278283

279-
class Body(Param):
284+
class Body(Param): # type: ignore[misc]
280285
_model = BodyModel
281286

282287

283-
class Form(Param):
288+
class Form(Param): # type: ignore[misc]
284289
_model = FormModel
285290

286291

287-
class File(Param):
292+
class File(Param): # type: ignore[misc]
288293
_model = FileModel
289294

290295

291-
class _MultiPartBody(Param):
296+
class _MultiPartBody(Param): # type: ignore[misc]
292297
_model = _MultiPartBodyModel
293298

294299
@classmethod

ninja/testing/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def __init__(self, http_response: Union[HttpResponse, StreamingHttpResponse]):
206206
if self.streaming:
207207
self.content = b"".join(http_response.streaming_content) # type: ignore
208208
else:
209-
self.content = http_response.content # type: ignore[union-attr]
209+
self.content = http_response.content
210210
self._data = None
211211

212212
def json(self) -> Any:

tests/test_orm_schemas.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ class Meta:
108108
"format": "date-time",
109109
},
110110
"decimalfield": {
111-
"anyOf": [{"type": "number"}, {"type": "string"}],
111+
"anyOf": [
112+
{"type": "number"},
113+
{"type": "string"},
114+
],
112115
"title": "Decimalfield",
113116
},
114117
"durationfield": {
@@ -199,6 +202,11 @@ class Meta:
199202
pydantic_version = tuple(map(int, pydantic.VERSION.split(".")[:2]))
200203
if pydantic_version >= (2, 11):
201204
expected_schema["properties"]["hstorefield"]["additionalProperties"] = True
205+
if pydantic_version >= (2, 12):
206+
# Pydantic 2.12 added pattern validation for decimal strings
207+
expected_schema["properties"]["decimalfield"]["anyOf"][1]["pattern"] = (
208+
r"^(?!^[-+.]*$)[+-]?0*\d*\.?\d*$"
209+
)
202210
assert SchemaCls.json_schema() == expected_schema
203211

204212

0 commit comments

Comments
 (0)