From 01e83ffeeace59f1acf45dedf597202ecf8a13d6 Mon Sep 17 00:00:00 2001 From: Waket Zheng Date: Thu, 4 Jan 2024 13:54:34 +0800 Subject: [PATCH] fix: pydantic v2.5 unittest error (#1535) (#1537) --- CHANGELOG.rst | 1 + tests/contrib/test_pydantic.py | 76 +++++++++++++++------------- tortoise/contrib/pydantic/creator.py | 3 ++ 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 07ff13885..6a9df2b72 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -21,6 +21,7 @@ Fixed ^^^^^ - Fix order of fields in `ValuesListQuery` when it has more than 10 fields. (#1492) - Fix pydantic v2 pydantic_model_creator nullable field not optional. (#1454) +- Fix pydantic v2.5 unittest error. (#1535) 0.20.0 ------ diff --git a/tests/contrib/test_pydantic.py b/tests/contrib/test_pydantic.py index 9c9c3a8fc..5475036eb 100644 --- a/tests/contrib/test_pydantic.py +++ b/tests/contrib/test_pydantic.py @@ -65,7 +65,7 @@ def test_event_schema(self): self.Event_Pydantic.model_json_schema(), { "$defs": { - "pydantic__main__tests__testmodels__Address__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": { "additionalProperties": False, "properties": { "city": {"maxLength": 64, "title": "City", "type": "string"}, @@ -81,7 +81,7 @@ def test_event_schema(self): "title": "Address", "type": "object", }, - "pydantic__main__tests__testmodels__Reporter__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": { "additionalProperties": False, "description": "Whom is assigned as the reporter", "properties": { @@ -97,7 +97,7 @@ def test_event_schema(self): "title": "Reporter", "type": "object", }, - "pydantic__main__tests__testmodels__Team__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": { "additionalProperties": False, "description": "Team that is a playing", "properties": { @@ -125,7 +125,7 @@ def test_event_schema(self): "title": "Team", "type": "object", }, - "pydantic__main__tests__testmodels__Tournament__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": { "additionalProperties": False, "properties": { "id": { @@ -164,20 +164,26 @@ def test_event_schema(self): "name": {"description": "The name", "title": "Name", "type": "string"}, "tournament": { "allOf": [ - {"$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf"} + { + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf" + } ], "description": "What tournaments is a happenin'", }, "reporter": { "anyOf": [ - {"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"}, + { + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf" + }, {"type": "null"}, ], "nullable": True, "title": "Reporter", }, "participants": { - "items": {"$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf"}, + "items": { + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf" + }, "title": "Participants", "type": "array", }, @@ -198,7 +204,9 @@ def test_event_schema(self): }, "address": { "anyOf": [ - {"$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf"}, + { + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf" + }, {"type": "null"}, ], "nullable": True, @@ -240,7 +248,7 @@ def test_eventlist_schema(self): "tournament": { "allOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf" } ], "description": "What tournaments is a happenin'", @@ -248,7 +256,7 @@ def test_eventlist_schema(self): "reporter": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf" }, {"type": "null"}, ], @@ -257,7 +265,7 @@ def test_eventlist_schema(self): }, "participants": { "items": { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf" }, "title": "Participants", "type": "array", @@ -287,7 +295,7 @@ def test_eventlist_schema(self): "address": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf" }, {"type": "null"}, ], @@ -309,7 +317,7 @@ def test_eventlist_schema(self): "title": "Event", "type": "object", }, - "pydantic__main__tests__testmodels__Address__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": { "additionalProperties": False, "properties": { "city": {"maxLength": 64, "title": "City", "type": "string"}, @@ -325,7 +333,7 @@ def test_eventlist_schema(self): "title": "Address", "type": "object", }, - "pydantic__main__tests__testmodels__Reporter__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": { "additionalProperties": False, "description": "Whom is assigned as the reporter", "properties": { @@ -341,7 +349,7 @@ def test_eventlist_schema(self): "title": "Reporter", "type": "object", }, - "pydantic__main__tests__testmodels__Team__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": { "additionalProperties": False, "description": "Team that is a playing", "properties": { @@ -369,7 +377,7 @@ def test_eventlist_schema(self): "title": "Team", "type": "object", }, - "pydantic__main__tests__testmodels__Tournament__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": { "additionalProperties": False, "properties": { "id": { @@ -422,7 +430,7 @@ def test_address_schema(self): "tournament": { "allOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf" } ], "description": "What tournaments is a happenin'", @@ -430,7 +438,7 @@ def test_address_schema(self): "reporter": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf" }, {"type": "null"}, ], @@ -439,7 +447,7 @@ def test_address_schema(self): }, "participants": { "items": { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf" }, "title": "Participants", "type": "array", @@ -480,7 +488,7 @@ def test_address_schema(self): "title": "Event", "type": "object", }, - "pydantic__main__tests__testmodels__Reporter__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": { "additionalProperties": False, "description": "Whom is assigned as the reporter", "properties": { @@ -496,7 +504,7 @@ def test_address_schema(self): "title": "Reporter", "type": "object", }, - "pydantic__main__tests__testmodels__Team__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": { "additionalProperties": False, "description": "Team that is a playing", "properties": { @@ -524,7 +532,7 @@ def test_address_schema(self): "title": "Team", "type": "object", }, - "pydantic__main__tests__testmodels__Tournament__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": { "additionalProperties": False, "properties": { "id": { @@ -588,7 +596,7 @@ def test_tournament_schema(self): "reporter": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf" }, {"type": "null"}, ], @@ -597,7 +605,7 @@ def test_tournament_schema(self): }, "participants": { "items": { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Team__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf" }, "title": "Participants", "type": "array", @@ -627,7 +635,7 @@ def test_tournament_schema(self): "address": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf" }, {"type": "null"}, ], @@ -648,7 +656,7 @@ def test_tournament_schema(self): "title": "Event", "type": "object", }, - "pydantic__main__tests__testmodels__Address__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": { "additionalProperties": False, "properties": { "city": {"maxLength": 64, "title": "City", "type": "string"}, @@ -664,7 +672,7 @@ def test_tournament_schema(self): "title": "Address", "type": "object", }, - "pydantic__main__tests__testmodels__Reporter__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": { "additionalProperties": False, "description": "Whom is assigned as the reporter", "properties": { @@ -680,7 +688,7 @@ def test_tournament_schema(self): "title": "Reporter", "type": "object", }, - "pydantic__main__tests__testmodels__Team__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Team__leaf": { "additionalProperties": False, "description": "Team that is a playing", "properties": { @@ -756,7 +764,7 @@ def test_team_schema(self): "tournament": { "allOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Tournament__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf" } ], "description": "What tournaments is a happenin'", @@ -764,7 +772,7 @@ def test_team_schema(self): "reporter": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf" }, {"type": "null"}, ], @@ -796,7 +804,7 @@ def test_team_schema(self): "address": { "anyOf": [ { - "$ref": "#/$defs/pydantic__main__tests__testmodels__Address__leaf" + "$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf" }, {"type": "null"}, ], @@ -817,7 +825,7 @@ def test_team_schema(self): "title": "Event", "type": "object", }, - "pydantic__main__tests__testmodels__Address__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Address__leaf": { "additionalProperties": False, "properties": { "city": {"maxLength": 64, "title": "City", "type": "string"}, @@ -833,7 +841,7 @@ def test_team_schema(self): "title": "Address", "type": "object", }, - "pydantic__main__tests__testmodels__Reporter__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf": { "additionalProperties": False, "description": "Whom is assigned as the reporter", "properties": { @@ -849,7 +857,7 @@ def test_team_schema(self): "title": "Reporter", "type": "object", }, - "pydantic__main__tests__testmodels__Tournament__leaf": { + "tortoise__contrib__pydantic__creator__tests__testmodels__Tournament__leaf": { "additionalProperties": False, "properties": { "id": { diff --git a/tortoise/contrib/pydantic/creator.py b/tortoise/contrib/pydantic/creator.py index 86cfeb5e4..87c08f096 100644 --- a/tortoise/contrib/pydantic/creator.py +++ b/tortoise/contrib/pydantic/creator.py @@ -132,6 +132,7 @@ def pydantic_model_creator( meta_override: Optional[Type] = None, model_config: Optional[ConfigDict] = None, validators: Optional[Dict[str, Any]] = None, + module: str = __name__, ) -> Type[PydanticModel]: """ Function to build `Pydantic Model `__ off Tortoise Model. @@ -158,6 +159,7 @@ def pydantic_model_creator( :param meta_override: A PydanticMeta class to override model's values. :param model_config: A custom config to use as pydantic config. :param validators: A dictionary of methods that validate fields. + :param module: The name of the module that the model belongs to. Note: Created pydantic model uses config_class parameter and PydanticMeta's 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]]: model = create_model( _name, __base__=PydanticModel, + __module__=module, __validators__=validators, **properties, )