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)

Most upvoted comments

If someone would try the work-around suggested at microsoft/pylance-release#5031 (comment) and report back, we could confirm or reject this hypothesis.

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 write field_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_ext works but pylance resolves it to Any) 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 RelatedManager from django_stubs_ext works for them in Pylance: https://github.com/typeddjango/django-stubs/issues/765#issuecomment-1854251782

If yes, please double check that you have the right version of both django-stubs and django-stubs-ext in whatever environment that Pylance runs with.

If that doesn’t help, post what errors you are getting.

from django_stubs_ext.db.models import manager and usingrelated: manager.RelatedManager[OtherModel] breaks Pylance type hints in VSCode

If we can figure out why pyright/pylance doesn’t like django_stubs_ext.db.models.manager.RelatedManager then we might be able to fix it. This would be the preferred solution. Unfortunately I don’t have much experience with pyright.

from django.db.models import manager and usingrelated: manager.RelatedManager[OtherModel] breaks Mypy, but only in the same file

Yep, that symbol no longer exists.

You may be able to resolve this by using from django.db.models.fields.related_descriptors import RelatedManager or similar, behind an if TYPE_CHECKING guard. But that’s a work-around at best.