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

Model.<related_name> has no attribute "through" #1800

Closed
michjnich opened this issue Oct 26, 2023 · 8 comments · Fixed by #1805
Closed

Model.<related_name> has no attribute "through" #1800

michjnich opened this issue Oct 26, 2023 · 8 comments · Fixed by #1805
Labels
bug Something isn't working mypy-plugin Issues specific to mypy_django_plugin

Comments

@michjnich
Copy link

Bug report

What's wrong

I confess, I am quite new to typing, so if this is a mypy issue and not a django-stubs issue, I apologise. I think I am reporting this in the right place though.

I have a ManyToManyField on a model:

class Post(models.Model):
   ...
    identifiers = models.ManyToManyField(Identifier, verbose_name=_("Identifiers"), related_name="posts")

And a TabularInline in admin for the Identifier model:

class PostInline(admin.TabularInline):
    model = Identifier.posts.through
    ...

But mypy does not like this :

error: "RelatedManager[Post]" has no attribute "through"  [attr-defined]

How is that should be

No error should be raised because Identifier.posts.through is valid

System information

  • OS: WSL2 under windows 11
    Linux LMAVSURFACE4 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
  • python version: 3.11
  • django version: 4.2.6
  • mypy version: 1.6.1
  • mypy-extensions version: 1.0.0
  • django-stubs version: 4.2.5
  • django-stubs-ext version: 4.2.5
@michjnich michjnich added the bug Something isn't working label Oct 26, 2023
@intgr
Copy link
Collaborator

intgr commented Oct 26, 2023

Hi! Did you configure mypy to use the mypy_django_plugin.main plugin? And did you configure a functional django_settings_module?

@michjnich
Copy link
Author

I have this in pyproject.toml

[tool.mypy]
python_version = "3.11"
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "config.settings"

@intgr
Copy link
Collaborator

intgr commented Oct 26, 2023

And does the INSTALLED_APPS in config.settings enable the app where your Post model exists?

@michjnich
Copy link
Author

It does, yes. (otherwise pretty much nothing would work :) )

@flaeppe
Copy link
Member

flaeppe commented Oct 26, 2023

I can reproduce this in test_many_to_many

- case: test_many_to_many
main: |
from myapp.models import MyModel, Other
reveal_type(MyModel.auto_through.through.objects.get())
reveal_type(MyModel().auto_through.get())
reveal_type(Other().autos.get())
reveal_type(MyModel.custom_through.through.objects.get())
reveal_type(MyModel().custom_through.get())
reveal_type(MyModel.custom_through.through.objects.custom_qs_method())
reveal_type(Other().customs.get())
auto_through = MyModel.auto_through.through.objects.get()
reveal_type(auto_through.id)
reveal_type(auto_through.pk)
reveal_type(auto_through.mymodel)
reveal_type(auto_through.mymodel_id)
reveal_type(auto_through.other)
reveal_type(auto_through.other_id)
reveal_type(MyModel.auto_through.through)
reveal_type(MyModel.auto_through.through.mymodel)
reveal_type(MyModel.auto_through.through._default_manager)
reveal_type(MyModel.other_again.through)
out: |

 -   case: test_many_to_many
    main: |
        from myapp.models import MyModel, Other
        ...

        reveal_type(MyModel.other_again.through)
+       reveal_type(Other.autos.through)

@flaeppe
Copy link
Member

flaeppe commented Oct 26, 2023

I think we need to improve with descriptor types here. e.g.

reveal_type(Other.autos)  # Revealed type is "django.db.models.manager.RelatedManager[myapp.models.MyModel]"
print(type(Other.autos))  # <class 'django.db.models.fields.related_descriptors.ManyToManyDescriptor'>

@superlevure
Copy link

I am still getting this error with 4.2.7

@flaeppe
Copy link
Member

flaeppe commented Dec 6, 2023

@superlevure feel free to provide a breaking case and/or open a new issue

@intgr intgr added the mypy-plugin Issues specific to mypy_django_plugin label Dec 8, 2023
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.

4 participants