Skip to content

Commit

Permalink
fixed mro error caused by typings (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
aledenza committed Jan 13, 2023
1 parent 300e687 commit d8fef5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
8 changes: 5 additions & 3 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
pydantic_queryset_creator,
)

from pydantic import BaseConfig as PydanticBaseConfig


class TestPydantic(test.TestCase):
async def asyncSetUp(self) -> None:
Expand Down Expand Up @@ -1074,7 +1076,7 @@ def test_override_meta_pydantic_config_by_model_creator(self):
model PydanticMeta's config_class.
"""

class AnotherConfigClass:
class AnotherConfigClass(PydanticBaseConfig):
title = "Another title!"

ModelPydantic = pydantic_model_creator(
Expand All @@ -1088,7 +1090,7 @@ class AnotherConfigClass:
def test_config_class_ignore_fields_config(self):
"""Generated config class should ignore config_class's fields parameter."""

class FieldsConfig:
class FieldsConfig(PydanticBaseConfig):
fields = ["id"]

ModelPydantic = pydantic_model_creator(
Expand All @@ -1103,7 +1105,7 @@ def test_config_classes_merge_all_configs(self):
- It merges (Default, Meta's config_class and creator's config_class) together.
"""

class MinLengthConfig:
class MinLengthConfig(PydanticBaseConfig):
min_anystr_length = 3

ModelPydantic = pydantic_model_creator(
Expand Down
4 changes: 2 additions & 2 deletions tortoise/contrib/pydantic/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import TYPE_CHECKING, List, Type, Union

import pydantic
from pydantic import BaseModel # pylint: disable=E0611
from pydantic import BaseModel, BaseConfig # pylint: disable=E0611

from tortoise import fields

Expand Down Expand Up @@ -45,7 +45,7 @@ class PydanticModel(BaseModel):
`model properties <https://pydantic-docs.helpmanual.io/usage/models/#model-properties>`__
"""

class Config:
class Config(BaseConfig):
orm_mode = True # It should be in ORM mode to convert tortoise data to pydantic

# noinspection PyMethodParameters
Expand Down
22 changes: 11 additions & 11 deletions tortoise/contrib/pydantic/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from hashlib import sha3_224
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, cast

import pydantic
from pydantic.main import BaseConfig as PydanticBaseConfig
from pydantic import BaseConfig as PydanticBaseConfig
from pydantic import Extra

from tortoise import fields
from tortoise.fields import relational, JSONField
from tortoise.contrib.pydantic.base import PydanticListModel, PydanticModel
from tortoise.contrib.pydantic.utils import get_annotations

Expand Down Expand Up @@ -246,10 +246,10 @@ def get_param(attr: str) -> Any:
# If at least one of default_config_class or config_class have extra,
# we don't add extra automatically.
if not hasattr(default_config_class, "extra") and not hasattr(config_class, "extra"):
pconfig_attrs["extra"] = pydantic.main.Extra.forbid
pconfig_attrs["extra"] = Extra.forbid

# Properties and their annotations` store
pconfig: Type[pydantic.main.BaseConfig] = type(
pconfig: Type[PydanticBaseConfig] = type(
"Config",
tuple(pconfig_bases),
pconfig_attrs,
Expand Down Expand Up @@ -368,9 +368,9 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:

# Foreign keys and OneToOne fields are embedded schemas
if (
field_type is fields.relational.ForeignKeyFieldInstance
or field_type is fields.relational.OneToOneFieldInstance
or field_type is fields.relational.BackwardOneToOneRelation
field_type is relational.ForeignKeyFieldInstance
or field_type is relational.OneToOneFieldInstance
or field_type is relational.BackwardOneToOneRelation
):
model = get_submodel(fdesc["python_type"])
if model:
Expand All @@ -383,8 +383,8 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:

# Backward FK and ManyToMany fields are list of embedded schemas
elif (
field_type is fields.relational.BackwardFKRelation
or field_type is fields.relational.ManyToManyFieldInstance
field_type is relational.BackwardFKRelation
or field_type is relational.ManyToManyFieldInstance
):
model = get_submodel(fdesc["python_type"])
if model:
Expand All @@ -398,7 +398,7 @@ def get_submodel(_model: "Type[Model]") -> Optional[Type[PydanticModel]]:
if annotation is not None:
pannotations[fname] = annotation
# Json fields
elif field_type is fields.JSONField:
elif field_type is JSONField:
pannotations[fname] = Any # type: ignore
# Any other tortoise fields
else:
Expand Down

0 comments on commit d8fef5f

Please sign in to comment.