Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
    but now, mypy rightly complains (because of LSP):
    class ObtainAuthTokenAndBringOverAnonymousProfiles(ObtainAuthToken):
        def post(self, request: stubs.Request, *args: Any, **kwargs: Any) -> Response:  # type: ignore[override]
    I have to ignore the override
    Does anyone have any resources to point me to for annotating third party libraries providing fields?
    We're now using Django Enumfields in our project, but the get/set types of the field are coming out as Any/Any
    When it should really be int/TheEnumYou'reUsing
    Nikita Sobolev
    @coiax that's an awesome question. Can you please open a new issue for this?
    hope that's what you were looking for
    David Grant
    @coiax just in case you weren't aware there is some better support for enums in Django now:
    Yeah, sadly we're currently stuck on Django 2.2 because one of our other third party libraries is using deprecated functionality
    Also, I don't know if django-stubs has been released for Django 3.0 yet
    Lachlan Cannon

    Hi, I've done a little googling but couldn't find anything relevant, apologies if I've overlooked.

    I'm looking to integrate mypy with django stubs into our mono-repo via sticklerci, and I don't know what I should be doing for the django_settings_module setting -- since it will be a different file depending on which project(s) it's running on.

    Is there a way to handle this, or should I be contacting stickler to ask for the ability to specify a different mypy.ini based on the repo path?
    @coiax : I think you can always type in manually
    like the days before Django-stubs was a thing
    my_field: ManualType = some_library.fields.CustomField()
    Anton Linevych

    Hi guys, is there any way to make this to work?

    class SomeManager(Manager.from_queryset(SomeQuerySet)):

    Right now mypy complains about Unsupported dynamic base class "Manager.from_queryset".

    Rory Byrne

    Hi folks, quick question (I'm relatively new to typed Python):

    Why is the package named with a hyphen - rest_framework-stubs? I want to do something like the below:

    from rest_framework-stubs.response import Response
    resp: Response = c.get('/')

    The problem here is that rest_framework-stubs is an illegal import due to the hyphen.

    What's going on? Should I even be using the stubs in this way?

    Lachlan Cannon
    I believe you should just be using DRF's own response class to type annotate
    from rest_framework.response import Response
    resp: Response = c.get('/')
    Rory Byrne
    @lcannon You're right, thanks.
    Unrelated: I see in the django-stubs README that Manager[MyModel] should be quoted as 'Manager[MyModel]' to avoid a type error, but this is not possible when subclassing. For example, I want to create a subclass class MyUserManager(BaseUserManager[MyUser]), but this triggers a TypeError. I cannot put it in quotes since it's inside the class definition. Is there any workaround while we wait for pull request 12405?
    Again please let me know if I'm mis-using Python typing here
    Rory Byrne
    The problem is caused by circular imports, since MyUser references MyManager, and MyManager references MyUser due to the necessity of including the [MyUser] typing in the MyManager definition
    I guess I'll just import it with django.apps. Or can I use a stub of MyUser to avoid importing MyUser?
    That doesn't work either because then I have an un-typed user: ??? = apps.get_model('auth', 'MyUser')
    Any ideas are welcome, apologies for the wall of text
    Rory Byrne
    I was being silly again. The correct way to subclass BaseUserManager is along the lines of T = TypeVar('T'); class MyUserManager(BaseUserManager[T])
    Rory Byrne
    ...And that's held up by PR 12405 - I'll wait for it to be merged
    Marti Raudsepp
    @RoryOfByrne You can use if TYPE_CHECKING as a work-around for the subclassing issue
    Or did you already find a solution?
    Marti Raudsepp
    Now that mypy 0.770 support is merged, any chance for a new release of django-stubs and drf-stubs?
    Marti Raudsepp
    For the record, version 1.5.0 was recently released:

    Trying to run mypy (0.770) during pre-commit and getting the following error:

    setup.cfg:10: error: Error importing plugin 'mypy_django_plugin.main': No module named 'mypy_django_plugin'

    django-stubs 1.5.0
    django 3.0.4

    Jose Di Nunzio
    Missing library? Try import mypy_django_plugin using the same python interpreter invoked by your pre-commit hook, it should fail. If that's the case, pip install it for that interpreter.
    Nikita Sobolev
    Try to use --show-traceback option
    (kind of late, but better than never!)
    Dustin Wyatt
    I'm sure I'm missing something obvious, but how do I avoid error: Missing type parameters for generic type "QuerySet" on custom QuerySet class definitions? just # type: ignore the line?
    Adrien Lemaire
    Hi there. Is it possible to fix this mypy error when getting model classes from ContentType (to avoid recursive looping between files imports) ?

    Hello guys, I got this error while trying to run pytest:

    ~/P/django-stubs ❯❯❯ pytest
    ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
    pytest: error: unrecognized arguments: --mypy-ini-file=./test-data/plugins.ini --mypy-extension-hook=scripts.tests_extension_hook.django_plugin_hook
      inifile: /Users/kacperszmigiel/Projects/django-stubs/pytest.ini
      rootdir: /Users/kacperszmigiel/Projects/django-stubs

    Has anyone encountered this error and knows a fix?

    Ok, it seems that I had so many Python versions installed on my machine that pytest went mad. Deleting them all and installing only 3.7 helped.
    I'm not sure if I'm missing something, but I get a mypy error when passing 'max_length' to a SlugField. It looks like the stub for SlugField is missing 'max_length' and 'min_length' parameters in init. I can submit an issue if it's not user error.
    Hi, my project extensively takes advantage of *_id syntax to access foreign key IDs. Does django-stubs support typing for such expressions?
    One obvious but cumbersome solution would be to manually add [foreign_key]_id field hint for every ForeignKey in every model, but even then I'm not sure what the appropriate type hint should be.
    Andri Janusson
    Hey people!
    Now that JSONField has moved from django.contrib.postgres.fields.JSONField to django.db.models in Django 3.1 (Under development) - how would you propose to continue for us using the development branch of Django?
    Just ignore it until it has been added to django-stubs?
    Melvyn Sopacua

    So, this is hard to explain:

    from django.contrib.auth.models import Group
    from django.db import models
    class MyGroup(Group):
    class User(models.Model):
        groups = models.ManyToManyField(MyGroup)
    group = MyGroup()
    user = User()
    reveal_type(user.groups) note: Revealed type is 'django.db.models.fields.related_descriptors.ManyToManyDescriptor' note: Revealed type is 'django.db.models.manager.RelatedManager*[django.contrib.auth.models.Permission]' note: Revealed type is 'Any'

    Any idea what's happening here?

    Nathan Levin-Greenhaw
    Anyone know when we can expect django-stubs@1.6.0 to get released?