Skip to content

Commit 01e83ff

Browse files
authored
fix: pydantic v2.5 unittest error (#1535) (#1537)
1 parent 6f18bb9 commit 01e83ff

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed
2121
^^^^^
2222
- Fix order of fields in `ValuesListQuery` when it has more than 10 fields. (#1492)
2323
- Fix pydantic v2 pydantic_model_creator nullable field not optional. (#1454)
24+
- Fix pydantic v2.5 unittest error. (#1535)
2425

2526
0.20.0
2627
------

tests/contrib/test_pydantic.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_event_schema(self):
6565
self.Event_Pydantic.model_json_schema(),
6666
{
6767
"$defs": {
68-
"pydantic__main__tests__testmodels__Address__leaf": {
68+
"tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": {
6969
"additionalProperties": False,
7070
"properties": {
7171
"city": {"maxLength": 64, "title": "City", "type": "string"},
@@ -81,7 +81,7 @@ def test_event_schema(self):
8181
"title": "Address",
8282
"type": "object",
8383
},
84-
"pydantic__main__tests__testmodels__Reporter__leaf": {
84+
"tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": {
8585
"additionalProperties": False,
8686
"description": "Whom is assigned as the reporter",
8787
"properties": {
@@ -97,7 +97,7 @@ def test_event_schema(self):
9797
"title": "Reporter",
9898
"type": "object",
9999
},
100-
"pydantic__main__tests__testmodels__Team__leaf": {
100+
"tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": {
101101
"additionalProperties": False,
102102
"description": "Team that is a playing",
103103
"properties": {
@@ -125,7 +125,7 @@ def test_event_schema(self):
125125
"title": "Team",
126126
"type": "object",
127127
},
128-
"pydantic__main__tests__testmodels__Tournament__leaf": {
128+
"tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": {
129129
"additionalProperties": False,
130130
"properties": {
131131
"id": {
@@ -164,20 +164,26 @@ def test_event_schema(self):
164164
"name": {"description": "The name", "title": "Name", "type": "string"},
165165
"tournament": {
166166
"allOf": [
167-
{"$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf"}
167+
{
168+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
169+
}
168170
],
169171
"description": "What tournaments is a happenin'",
170172
},
171173
"reporter": {
172174
"anyOf": [
173-
{"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"},
175+
{
176+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
177+
},
174178
{"type": "null"},
175179
],
176180
"nullable": True,
177181
"title": "Reporter",
178182
},
179183
"participants": {
180-
"items": {"$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf"},
184+
"items": {
185+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf"
186+
},
181187
"title": "Participants",
182188
"type": "array",
183189
},
@@ -198,7 +204,9 @@ def test_event_schema(self):
198204
},
199205
"address": {
200206
"anyOf": [
201-
{"$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf"},
207+
{
208+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf"
209+
},
202210
{"type": "null"},
203211
],
204212
"nullable": True,
@@ -240,15 +248,15 @@ def test_eventlist_schema(self):
240248
"tournament": {
241249
"allOf": [
242250
{
243-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf"
251+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
244252
}
245253
],
246254
"description": "What tournaments is a happenin'",
247255
},
248256
"reporter": {
249257
"anyOf": [
250258
{
251-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
259+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
252260
},
253261
{"type": "null"},
254262
],
@@ -257,7 +265,7 @@ def test_eventlist_schema(self):
257265
},
258266
"participants": {
259267
"items": {
260-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf"
268+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf"
261269
},
262270
"title": "Participants",
263271
"type": "array",
@@ -287,7 +295,7 @@ def test_eventlist_schema(self):
287295
"address": {
288296
"anyOf": [
289297
{
290-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf"
298+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf"
291299
},
292300
{"type": "null"},
293301
],
@@ -309,7 +317,7 @@ def test_eventlist_schema(self):
309317
"title": "Event",
310318
"type": "object",
311319
},
312-
"pydantic__main__tests__testmodels__Address__leaf": {
320+
"tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": {
313321
"additionalProperties": False,
314322
"properties": {
315323
"city": {"maxLength": 64, "title": "City", "type": "string"},
@@ -325,7 +333,7 @@ def test_eventlist_schema(self):
325333
"title": "Address",
326334
"type": "object",
327335
},
328-
"pydantic__main__tests__testmodels__Reporter__leaf": {
336+
"tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": {
329337
"additionalProperties": False,
330338
"description": "Whom is assigned as the reporter",
331339
"properties": {
@@ -341,7 +349,7 @@ def test_eventlist_schema(self):
341349
"title": "Reporter",
342350
"type": "object",
343351
},
344-
"pydantic__main__tests__testmodels__Team__leaf": {
352+
"tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": {
345353
"additionalProperties": False,
346354
"description": "Team that is a playing",
347355
"properties": {
@@ -369,7 +377,7 @@ def test_eventlist_schema(self):
369377
"title": "Team",
370378
"type": "object",
371379
},
372-
"pydantic__main__tests__testmodels__Tournament__leaf": {
380+
"tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": {
373381
"additionalProperties": False,
374382
"properties": {
375383
"id": {
@@ -422,15 +430,15 @@ def test_address_schema(self):
422430
"tournament": {
423431
"allOf": [
424432
{
425-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf"
433+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
426434
}
427435
],
428436
"description": "What tournaments is a happenin'",
429437
},
430438
"reporter": {
431439
"anyOf": [
432440
{
433-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
441+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
434442
},
435443
{"type": "null"},
436444
],
@@ -439,7 +447,7 @@ def test_address_schema(self):
439447
},
440448
"participants": {
441449
"items": {
442-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf"
450+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf"
443451
},
444452
"title": "Participants",
445453
"type": "array",
@@ -480,7 +488,7 @@ def test_address_schema(self):
480488
"title": "Event",
481489
"type": "object",
482490
},
483-
"pydantic__main__tests__testmodels__Reporter__leaf": {
491+
"tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": {
484492
"additionalProperties": False,
485493
"description": "Whom is assigned as the reporter",
486494
"properties": {
@@ -496,7 +504,7 @@ def test_address_schema(self):
496504
"title": "Reporter",
497505
"type": "object",
498506
},
499-
"pydantic__main__tests__testmodels__Team__leaf": {
507+
"tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": {
500508
"additionalProperties": False,
501509
"description": "Team that is a playing",
502510
"properties": {
@@ -524,7 +532,7 @@ def test_address_schema(self):
524532
"title": "Team",
525533
"type": "object",
526534
},
527-
"pydantic__main__tests__testmodels__Tournament__leaf": {
535+
"tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": {
528536
"additionalProperties": False,
529537
"properties": {
530538
"id": {
@@ -588,7 +596,7 @@ def test_tournament_schema(self):
588596
"reporter": {
589597
"anyOf": [
590598
{
591-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
599+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
592600
},
593601
{"type": "null"},
594602
],
@@ -597,7 +605,7 @@ def test_tournament_schema(self):
597605
},
598606
"participants": {
599607
"items": {
600-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf"
608+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf"
601609
},
602610
"title": "Participants",
603611
"type": "array",
@@ -627,7 +635,7 @@ def test_tournament_schema(self):
627635
"address": {
628636
"anyOf": [
629637
{
630-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf"
638+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf"
631639
},
632640
{"type": "null"},
633641
],
@@ -648,7 +656,7 @@ def test_tournament_schema(self):
648656
"title": "Event",
649657
"type": "object",
650658
},
651-
"pydantic__main__tests__testmodels__Address__leaf": {
659+
"tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": {
652660
"additionalProperties": False,
653661
"properties": {
654662
"city": {"maxLength": 64, "title": "City", "type": "string"},
@@ -664,7 +672,7 @@ def test_tournament_schema(self):
664672
"title": "Address",
665673
"type": "object",
666674
},
667-
"pydantic__main__tests__testmodels__Reporter__leaf": {
675+
"tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": {
668676
"additionalProperties": False,
669677
"description": "Whom is assigned as the reporter",
670678
"properties": {
@@ -680,7 +688,7 @@ def test_tournament_schema(self):
680688
"title": "Reporter",
681689
"type": "object",
682690
},
683-
"pydantic__main__tests__testmodels__Team__leaf": {
691+
"tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": {
684692
"additionalProperties": False,
685693
"description": "Team that is a playing",
686694
"properties": {
@@ -756,15 +764,15 @@ def test_team_schema(self):
756764
"tournament": {
757765
"allOf": [
758766
{
759-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf"
767+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf"
760768
}
761769
],
762770
"description": "What tournaments is a happenin'",
763771
},
764772
"reporter": {
765773
"anyOf": [
766774
{
767-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
775+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
768776
},
769777
{"type": "null"},
770778
],
@@ -796,7 +804,7 @@ def test_team_schema(self):
796804
"address": {
797805
"anyOf": [
798806
{
799-
"$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf"
807+
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf"
800808
},
801809
{"type": "null"},
802810
],
@@ -817,7 +825,7 @@ def test_team_schema(self):
817825
"title": "Event",
818826
"type": "object",
819827
},
820-
"pydantic__main__tests__testmodels__Address__leaf": {
828+
"tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": {
821829
"additionalProperties": False,
822830
"properties": {
823831
"city": {"maxLength": 64, "title": "City", "type": "string"},
@@ -833,7 +841,7 @@ def test_team_schema(self):
833841
"title": "Address",
834842
"type": "object",
835843
},
836-
"pydantic__main__tests__testmodels__Reporter__leaf": {
844+
"tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": {
837845
"additionalProperties": False,
838846
"description": "Whom is assigned as the reporter",
839847
"properties": {
@@ -849,7 +857,7 @@ def test_team_schema(self):
849857
"title": "Reporter",
850858
"type": "object",
851859
},
852-
"pydantic__main__tests__testmodels__Tournament__leaf": {
860+
"tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": {
853861
"additionalProperties": False,
854862
"properties": {
855863
"id": {

tortoise/contrib/pydantic/creator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def pydantic_model_creator(
132132
meta_override: Optional[Type] = None,
133133
model_config: Optional[ConfigDict] = None,
134134
validators: Optional[Dict[str, Any]] = None,
135+
module: str = __name__,
135136
) -> Type[PydanticModel]:
136137
"""
137138
Function to build `Pydantic Model <https://pydantic-docs.helpmanual.io/usage/models/>`__ off Tortoise Model.
@@ -158,6 +159,7 @@ def pydantic_model_creator(
158159
:param meta_override: A PydanticMeta class to override model's values.
159160
:param model_config: A custom config to use as pydantic config.
160161
:param validators: A dictionary of methods that validate fields.
162+
:param module: The name of the module that the model belongs to.
161163
162164
Note: Created pydantic model uses config_class parameter and PydanticMeta's
163165
config_class as its Config class's bases(Only if provided!), but it
@@ -441,6 +443,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
441443
model = create_model(
442444
_name,
443445
__base__=PydanticModel,
446+
__module__=module,
444447
__validators__=validators,
445448
**properties,
446449
)

0 commit comments

Comments
 (0)