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

ContentType.app_label is typed as Any by mypy #2446

Open
mthuurne opened this issue Nov 19, 2024 · 2 comments
Open

ContentType.app_label is typed as Any by mypy #2446

mthuurne opened this issue Nov 19, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@mthuurne
Copy link
Contributor

Bug report

What's wrong

If I run mypy on this code:

from django.contrib.contenttypes.models import ContentType
from django.db import models

def f(model: type[models.Model]) -> None:
    ct = ContentType.objects.get_for_model(model)
    reveal_type(ct)
    reveal_type(ct.app_label)

It outputs:

testcase.py:6: note: Revealed type is "django.contrib.contenttypes.models.ContentType"
testcase.py:7: note: Revealed type is "Any"

In django-stubs/contrib/contenttypes/models.pyi, the ContentType class is annotated as follows:

class ContentType(models.Model):
    id: int
    app_label: models.CharField
    # (snip)

So the type for ct is inferred as expected, but for the app_label field it is not, even though that field is annotated in the stubs. Is this some bug or limitation of the plugin?

How is that should be

The runtime type for ct.app_label is str, so ideally that would be the inferred type.

System information

  • OS:
  • python version: 3.10.12
  • django version: 4.2
  • mypy version: 1.13.0
  • django-stubs version: 5.1.1
  • django-stubs-ext version: 5.1.1
@mthuurne mthuurne added the bug Something isn't working label Nov 19, 2024
@sobolevn
Copy link
Member

PR is welcome

@mthuurne
Copy link
Contributor Author

I could use some hints on where to start looking. Is it expected or unexpected that the type for app_label isn't inferred? And would the solution indeed be to change the plugin?

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

No branches or pull requests

2 participants