Skip to content

Invalid type hint for CharEnumField in generated migration script #2155

@kinnay

Description

@kinnay

Describe the bug
If I generate a migration file with tortoise migrate for a model that contains a CharEnumField, and then run mypy on my project, it produces an error. The reason seems to be that CharEnumField does not return an instance of Field.

When the migration creates a new table (ops.CreateModel) the following error is printed:

error: List item 1 has incompatible type "tuple[str, ExampleEnum]"; expected "tuple[str, Field[Any] | ManyToManyRelation[Any] | None]"  [list-item]

When the migration adds a CharEnumField to an existing table, the following error is printed:

error: Argument "field" to "AddField" has incompatible type "ExampleEnum"; expected "Field[Any] | ManyToManyRelation[Any] | None"  [arg-type]

To Reproduce
Below is a minimal reproducible example.

models.py:

from tortoise.fields import CharEnumField
from tortoise.models import Model

import enum


class ExampleEnum(enum.StrEnum):
    Value1 = enum.auto()
    Value2 = enum.auto()


class ExampleModel(Model):
    field: ExampleEnum = CharEnumField(ExampleEnum)

config.json:

{
    "connections": {
        "default": "sqlite://:memory:"
    },
    "apps": {
        "monitor": {
            "models": ["models"],
            "migrations": "migrations",
            "default_connection": "default"
        }
    }
}

Steps to reproduce:

  1. tortoise --config-file config.json makemigrations
  2. mypy migrations

Expected behavior
No mypy errors on scripts that are generated by Tortoise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions