django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Use `stubgen` to extract precise type signatures for use by other tools?

Open psacawa opened this issue 2 years ago • 1 comments

Hi all,

Like many of you, I'm chasing a better editor experience for Django. Unfortunately, mypy doens't implement a LSP server, so its type information can't be directly consumed in the editor to e.g. respond to completion requests (notwithstanding things like mypyls, which seem to just expose mypy diagnostics to the editor).

So I use another python language server pyright which doesn't support mypy plugins, and therefore can't use mypy_django_plugin. So there are no reverse accessors etc..

I had the following idea to use mypy_django_plugin with pyright:

  1. Use mypy's stubgen command with the mypy_django_plugin to create the "enhanced" stubs with reverse accessors etc..
  2. Get pyright to read these stubs.
  3. Enjoy smart editor completion for things like author.book_set.all() etc...

So far, I can't get step 1 to work. The Django models' stubs aren't smart. They are like:

class Author(models.Model):
  name: Any
  age: Any
  ...

Has any one tried this strategy? Could it work in principle? This is not an obscure use case, so someone must have tried something.

EDIT: Reading more, it's seems like stubgen won't do this. Nevertheless, does one have an idea how to get type information out of mypy?

psacawa avatar Jun 22 '22 08:06 psacawa

This makes sense me (not sure if this would happen in this projects or not) - but we need completion to work with LSP, somehow.

I hope this remains unlocked so people can chip in a link if a project / effort comes up.

tony avatar Aug 01 '22 18:08 tony