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

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

Closed
Alexerson opened this issue Jun 16, 2023 · 0 comments · Fixed by #1719
Closed

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

Alexerson opened this issue Jun 16, 2023 · 0 comments · Fixed by #1719
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin

Comments

@Alexerson
Copy link
Contributor

Alexerson commented Jun 16, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin
Development

Successfully merging a pull request may close this issue.

2 participants