Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pydantic typing fix #1316

Merged
merged 1 commit into from Jan 13, 2023
Merged

Conversation

aledenza
Copy link
Contributor

@aledenza aledenza commented Jan 5, 2023

Description

Changed config type for pydantic model creator.

Motivation and Context

I faced MRO issue when i tried to create type-correct config class for pydantic_model_creator.
Example

from tortoise import fields
from tortoise.models import Model
from pydantic import BaseConfig


def to_upper(s: str) -> str:
    return s.upper()


class ConfigClass(BaseConfig):
    alias_generator = to_upper
    allow_population_by_field_name = True


class ModelClass(Model):
    name: str = fields.TextField()
    age: int = fields.IntField()


pydantic_model = pydantic_model_creator(
    ModelClass,
    name="get_model",
    config_class=ConfigClass,
)

If you try to run this code you will face MRO issue

Cannot create a consistent method resolution
order (MRO) for bases BaseConfig, Config

If ConfigClass does not inherits BaseConfig everything works fine, but typing is incorrect. So, this MR main aim is to fix some typing errors.

How Has This Been Tested?

  1. Changed some tests to make config classes inherit BaseConfig
  2. To check code style and correct work used make check and make test.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@aledenza
Copy link
Contributor Author

@long2ice , hello. I rebased my branch to current develop, so all check have passed. What I have to do next? This MR should not affect any workability, because it's basically typehinting improvements. Plus it fixes possible MRO exception.
If this MR needs more explanation or improvements, I'm ready

@long2ice long2ice merged commit d8fef5f into tortoise:develop Jan 13, 2023
@long2ice
Copy link
Member

Thanks!

waketzheng pushed a commit to waketzheng/tortoise-orm that referenced this pull request Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants