Skip to content

Commit

Permalink
fix: pydantic v2.5 unittest error (#1535) (#1537)
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng authored Jan 4, 2024
1 parent 6f18bb9 commit 01e83ff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
76 changes: 42 additions & 34 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
},
Expand All @@ -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,
Expand Down Expand Up @@ -240,15 +248,15 @@ 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'",
},
"reporter": {
"anyOf": [
{
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
},
{"type": "null"},
],
Expand All @@ -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",
Expand Down Expand Up @@ -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"},
],
Expand All @@ -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"},
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -422,15 +430,15 @@ 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'",
},
"reporter": {
"anyOf": [
{
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
},
{"type": "null"},
],
Expand All @@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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"},
],
Expand All @@ -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",
Expand Down Expand Up @@ -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"},
],
Expand All @@ -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"},
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -756,15 +764,15 @@ 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'",
},
"reporter": {
"anyOf": [
{
"$ref": "#/$defs/pydantic__main__tests__testmodels__Reporter__leaf"
"$ref": "#/$defs/tortoise__contrib__pydantic__creator__tests__testmodels__Reporter__leaf"
},
{"type": "null"},
],
Expand Down Expand Up @@ -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"},
],
Expand All @@ -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"},
Expand All @@ -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": {
Expand All @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://pydantic-docs.helpmanual.io/usage/models/>`__ off Tortoise Model.
Expand All @@ -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
Expand Down Expand Up @@ -441,6 +443,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
model = create_model(
_name,
__base__=PydanticModel,
__module__=module,
__validators__=validators,
**properties,
)
Expand Down

0 comments on commit 01e83ff

Please sign in to comment.