Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Wojciech Niedźwiedź
    @Niedzwiedzw
    that's what happens when I'm running "mypy"
    [mypy]
    files=wwau,../../shared
    ignore_missing_imports=true
    plugins =
    mypy_django_plugin.main
    how do I even debug this? the app runs fine
    Na'aman Hirschfeld
    @Goldziher
    hiya @Niedzwiedzw , have you added the reference to the django settings in the mypy.ini?
    if you read the package's docs, you should add a section like so:
    "[mypy.plugins.django-stubs]
    django_settings_module = your_django_settings"
    hope this helps.
    Wojciech Niedźwiedź
    @Niedzwiedzw

    [mypy]
    files=wwau,../../shared
    ignore_missing_imports=true
    plugins =
    mypy_django_plugin.main

    [mypy.plugins.django-stubs]
    django_settings_module = "wwau.settings"

    sorry, it got trimmed as I was pasting @Goldziher
    it was there all along yes
    proxi
    @void-witch

    i've got a class-based login view that overrides FormView and uses an AuthenticationForm. the idiomatic django way to use this is to override form_valid to log the user in:

    def form_valid(self, form):
        auth_login(self.request, form.get_user())

    the issue is that FormView uses a BaseForm which doesn't have a get_user method, and casting form to AuthenticationForm violates liskov
    is there a better, type-safe way to log in the user without querying the database?

    proxi
    @void-witch
    nevermind, django has a generic loginview class
    Harold Mendoza
    @sepira
    Trying django-stubs seems have this error: Error importing plugin 'mypy_django_plugin.main': No module named 'django.utils'
    proxi
    @void-witch
    try reinstalling django? seems like a broken (virtual) environment
    Harold Mendoza
    @sepira
    @3n-k1 probably, but worked fine on docker container so no issues
    Harold Mendoza
    @sepira

    I have a function that is written before the model:

    def a_func(name: str) -> Model1: return ContentType.objects.get( model=name, ).model_class()

    class Model1(Model): ...

    The question is why this works:

    def a_func(name: str) -> 'Model1' //wrapped with string literal
    but not without the string literal?

    Robot Wei
    @wyl8899
    yes, quoting the type hint makes "forward declaration" possible
    caseydm
    @caseydm_gitlab
    Is there a changelog available for recent versions of django-stubs?
    proxi
    @void-witch
    why does the travis configuration require libproj-dev? i'm super curious
    proxi
    @void-witch
    ah, for testing django's gis stuff
    beautiful-boyyy
    @beautiful-boyyy
    image.png
    image.png
    Is there anyone can help me? I configed the setup.cfg, but the pyright still not work well
    It gived me the wrong promote
    here is my work directory tree
    image.png
    beautiful-boyyy
    @beautiful-boyyy
    It gived me the wrong prompt, sorry I typed wrong words just now...
    Ihor
    @fatalispm
    Hi, I am trying to add this awesome library to our project, but unfortunately, we rely heavily on the .as_manager() functionality. I have seen that @kszmigiel was making efforts to implement it. I understand that this is a very complicated issue, so I wonder if there are workarounds .
    Konstantin Alekseev
    @kalekseev
    @fatalispm hi, you can sacrifice queryset's strict typing to workaround this, patch: kalekseev/django-stubs@38ac3c8 would be great to have ability to configure django-stubs to disable strict querysets typings until we have as_manager/as_queryset support.
    Ihor
    @fatalispm

    @kalekseev thanks for your reply! Agree, this option would be great. In that case, there would be no field checks for this manager, right? Tbh, it looks that creating your own manager with a copypasted queryset methods seems to help.

    class Q(models.Queryset[User]):
      def active(): ..
    
    class M(models.Manager[User]):
     def active():
         return self.get_queryset().active()

    This is a little bit annoying, but in that case, it looks that mypy is able to detect errors here.

    How hard do you think it will be to add support for as.manager()?

    Konstantin Alekseev
    @kalekseev

    @fatalispm yes, with that patch your managers will be typed as Any. Of course you can provide types by hands but I don't think it's an acceptable method for a big codebase.

    How hard do you think it will be to add support for as.manager()?

    I think previous attempt typeddjango/django-stubs#272 failed because of bug (limitation) in mypy python/mypy#8359

    Ihor
    @fatalispm

    @kalekseev That's interesting. I didn't mean providing types by hand, but using Manager that will duplicate queryset methods.. (e.g living without queryset.as_manager()). This adds boilerplate, but on the other hand, it can potentially eliminate a few bugs?

    So do you think that get_dynamic_class_hook will fix the problem with .as_manager()?

    Konstantin Alekseev
    @kalekseev
    @fatalispm I don't think my knowledge about current state of django-stubs and mypy is up to date, better ask someone who worked on that recently.
    Seth Yastrov
    @syastrov
    @fatalispm @kalekseev It seems like it's possible to work around it by using as_queryset (I tested with django-stubs 0.7.0). Perhaps this should be documented, as it seems like a pretty common pitfall.
    from django.db import models
    class MyModelQuerySet(models.QuerySet):
      def foo(): pass
    
    MyModelManager = models.Manager.from_queryset(MyModelQuerySet)
    
    class MyModel(models.Model):
      objects = MyModelManager()
    
    def use_my_model():
        m = MyModel.objects.get(id=1)
        reveal_type(m)  # MyModel
    1 reply
    Alex
    @slothyrulez
    hi
    good morning (from spain)
    i'm wondering if exists a way to express a model typing without importing it
    the same way as fuu: "QuerySet[FUUModel]"
    something like fuu: "Model[FUUModel]"
    def send(self, *, communication: "Model[Communication]", schedule: "Model[CommunicationSchedule]"): ...
    Alex
    @slothyrulez
    thanks
    Alex
    @slothyrulez
    for the future readers (and the future me)
    # a.py
    from typing import TYPE_CHECKING
    if TYPE_CHECKING:
        from .b import B
    
    
    class A:
        b: "B"
    
        def __init__(self, b: "B"):
            self.b = b
    Gustavo Castellanos
    @gcastellanos1_gitlab
    Hi, new here. I have one question: I wrote a decorator for my models in Django, and it works. But when I started using type hints, stating that the argument is of type Model, I got this error: "Type[ModelBase]" is incompatible with "Type[Model]" in the line where I use my decorator. Something like this:
    @my_decorator
    class MyModel(models.Model):
    ...
    Kyle Bebak
    @kylebebak

    Hey all. Has anyone tried using these type stubs with a different type checker, like pyright? I understand that django-stubs ships with a mypy plugin, and the README says it doesn't make sense to try to use the stubs without mypy

    That said, I was hoping I could get some of the benefits of using the type stubs in django-stubs without using the mypy plugin

    @sobolevn Is it possible to get a useful subset of the benefits of the stubs with a type checker other than mypy?

    1 reply
    Marti Raudsepp
    @intgr
    It should be possible, yes. I initially used django-stubs with mypy without the plugin. You lose out on a lot of things involving the Django ORM. Some of those can be compensated with addition of manual type hints, but other things can't.
    1 reply
    caseydm
    @caseydm
    Hi all. When you type check a django request object, do you use HttpRequest or WsgiRequest?
    Jacob
    @Jacob25585268_twitter
    hey all. I find some pyi files which don't match the source files, which are they used for? (eg. django-stubs/contrib/admin/templatetags/admin_static.pyi, django-stubs/contrib/auth/management/commands/init.pyi
    1 reply
    Gourav Sardana
    @GouravSardana
    Hey Everyone