django-stubs: RelatedManager import moved breaking Pylance
Bug report
What’s wrong
from django_stubs_ext.db.models import manager and usingrelated: manager.RelatedManager[OtherModel] breaks Pylance type hints in VSCode
from django.db.models import manager and usingrelated: manager.RelatedManager[OtherModel] breaks Mypy, but only in the same file
How is that should be
- I know that there were improvements on RelatedManager automatic typing
- We have always used Mypy as the source of truth for our types, so I guess we will use the new manager type, but this breaks VSCode completion, so it’ a bit sad
- Is there a way to have
from django_stubs_ext.db.models import manager correctly export Django Manager so autocomplete works?
System information
- OS:
python version: 3.12.0
django version: 4.2.8
mypy version: 1.7.1
django-stubs version: 4.2.7
django-stubs-ext version: 4.2.7
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 7
- Comments: 15 (6 by maintainers)
I’ve tried the workaround. Pylance then correctly picks installed django-stubs instead of the bundled django-types, and RelatedManager can be imported from django_stubs_ext.db.models.manager.
However, after being switched to django-stubs, Pylance lost the inference for the fields’ types. I have to explicitly write the type hints for ForeignKey like
author = models.ForeignKey[Author, Author](Author, on_delete=models.CASCADE), otherwise author will be inferred to Unknown (Pylance with type checking mode set to basic). Even for PositiveSmallIntegerField, I have to writefield_a = models.PositiveSmallIntegerField[int, int], or the type field_a will also be inferred to Unknown. If I’ll have to write type hints for every field, it will be too tedious.@intgr
I just encountered this problem (importing from
django_stubs_extworks but pylance resolves it toAny) and re-installing the extension (Pylance) fixed it for me.This works as in, it doesn’t cause pylance to crash… but pylance resolves it to ANY
Are you still experiencing this issue? Another user reported that
RelatedManagerfromdjango_stubs_extworks for them in Pylance: https://github.com/typeddjango/django-stubs/issues/765#issuecomment-1854251782If yes, please double check that you have the right version of both
django-stubsanddjango-stubs-extin whatever environment that Pylance runs with.If that doesn’t help, post what errors you are getting.
If we can figure out why pyright/pylance doesn’t like
django_stubs_ext.db.models.manager.RelatedManagerthen we might be able to fix it. This would be the preferred solution. Unfortunately I don’t have much experience with pyright.Yep, that symbol no longer exists.
You may be able to resolve this by using
from django.db.models.fields.related_descriptors import RelatedManageror similar, behind anif TYPE_CHECKINGguard. But that’s a work-around at best.