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

through attribute not defined within model definition #1940

Closed
mfosterw opened this issue Feb 13, 2024 · 0 comments · Fixed by #2026
Closed

through attribute not defined within model definition #1940

mfosterw opened this issue Feb 13, 2024 · 0 comments · Fixed by #2026
Labels
bug Something isn't working

Comments

@mfosterw
Copy link
Contributor

mfosterw commented Feb 13, 2024

Bug report

What's wrong

Mypy cannot detect the through attribute of a ManyToManyField within the definition of the enclosing model. To reproduce, add the following test method to MyModel in tests/typecheck/fields/test_related.yml::test_many_to_many:

class MyModel(models.Model):
    auto_through = models.ManyToManyField(Other, related_name="autos")
    # Have multiple M2Ms with implicit through
    other_again = models.ManyToManyField(Other, related_name="others_again")
    custom_through = models.ManyToManyField(Other, through=CustomThrough, related_name="customs")

    objects = MyModelManager()

    def test(self) -> None:
        reveal_type(self.custom_through.through)

Results in:

myapp/models/__init__:31: error: "Other_ManyRelatedManager" has no attribute "through"  [attr-defined]
myapp/models/__init__:31: note: Revealed type is "Any"

How is that should be

Revealed type should be "CustomThrough"

System information

  • OS: macOS 14.2.1
  • python version: 3.12.1
  • django version: 4.2.9
  • mypy version: 1.7.1
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

1 participant