tortoise / tortoise/tortoise-orm

Incompatible types in assignment

Open
#484 7 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Mypy throws an error if I try to assign value to a model field.

To Reproduce

class MyModel(tortoise.Model):
    ...
    my_field = fields.BooleanField(default=False)

m = await MyModel.get(id=...)
m.deleted = True
await m.save()

>>> Mypy error: Incompatible types in assignment (expression has type "bool", variable has type "BooleanField") 

Expected behavior
No mypy errors =)

Additional context
In django there is a similar problem and I should disable type check entirely or use django-stubs.

Tortoise, on the other hand, uses typing module heavily. Is there something that I can use to avoid such errors?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the MyModel example with a BooleanField assignment and running mypy. Investigate how model fields are typed and exposed to instances. Done means valid assignments such as m.deleted = True produce no mypy error while the model can still be saved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, databases
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.