Skip to content

Many-to-Many generated through model are not properly typed #1549

@Alexerson

Description

@Alexerson

Bug report

The generated through model for many-to-many is not getting typed properly.

What's wrong

In the following example:

from typing import reveal_type
from django.db import models


class Topping(models.Model):
    label = models.CharField(max_length=100)


class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)


for toppings_through in Pizza.toppings.through.objects.all():
    reveal_type(toppings_through)
    reveal_type(toppings_through.pizza)
    reveal_type(toppings_through.topping)

I would expect the revealed types to be:

  • Not sure, but at least Model
  • Pizza
  • Topping

but the result is:

demo.py:14: note: Revealed type is "Any"
demo.py:15: note: Revealed type is "Any"
demo.py:16: note: Revealed type is "Any"

reveal_type(Pizza.toppings.through) gives Type[django.db.models.base.Model], so I’m assuming that’s where we could be more precise.

System information

  • OS:
  • python version: 3.11.1
  • django version: 4.2.2
  • mypy version: 1.3.0
  • django-stubs version: 4.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmypy-pluginIssues specific to mypy_django_plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions