django-stubs
django-stubs copied to clipboard
Use `stubgen` to extract precise type signatures for use by other tools?
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
:
- Use mypy's
stubgen
command with themypy_django_plugin
to create the "enhanced" stubs with reverse accessors etc.. - Get
pyright
to read these stubs. - 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
?
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.