Autocomplete not working properly for inherited classes - Ubuntu
Issue Type: Bug
Behaviour
Expected vs. Actual
While working on a Django project, autocompleting doesn't seem to be working properly for inhereted classes.
from django.contrib.auth.models import User, auth user = User.objects.create_user(username=user_name, password=password1, email=email, first_name=first_name, last_name=last_name)
if I try to type user. nothing is being suggested.

Also, when I instated the object, when I type in User.objects create_user is not showing.

Steps to reproduce:
from django.contrib.auth.models import User, auth user = User.objects.create_user(username=user_name, password=password1, email=email, first_name=first_name, last_name=last_name)
Also, I don't get syntax error when I type in incorrect method name

Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.10.4
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
- Value of the
python.languageServersetting: Default
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
XXX
User Settings
venvPath: "<placeholder>"
languageServer: "Pylance"
Extension version: 2022.6.2 VS Code version: Code 1.67.2 (c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5, 2022-05-17T18:23:40.286Z) OS version: Linux x64 5.15.0-33-generic Restricted Mode: No
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (4 x 2592) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on oop_rasterization: disabled_off opengl: enabled_on rasterization: disabled_software raw_draw: disabled_off_ok skia_renderer: enabled_on video_decode: disabled_software video_encode: disabled_software vulkan: disabled_off webgl: enabled webgl2: enabled |
| Load (avg) | 0, 1, 0 |
| Memory (System) | 3.80GB (0.24GB free) |
| Process Argv | --crash-reporter-id 9c803422-b6ae-465a-a25d-e15097affe9e |
| Screen Reader | no |
| VM | 100% |
| DESKTOP_SESSION | ubuntu |
| XDG_CURRENT_DESKTOP | Unity |
| XDG_SESSION_DESKTOP | ubuntu |
| XDG_SESSION_TYPE | wayland |
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyl392:30443607
pythontb:30283811
pythonptprofiler:30281270
vshan820:30294714
vstes263cf:30335440
pythondataviewer:30285071
vscod805:30301674
pythonvspyt200:30340761
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
vsc1dst:30438360
pythonvs932:30410667
wslgetstarted:30449410
pythonvsnew555:30457759
vscscmwlcmt:30465135
cppdebug:30492333
vscaat:30438848
vsclangdc:30486549
Please provide a self-contained minimal code sample that demonstrates the problem. It's difficult to diagnose problems from partial screen shots. Also, please provide the versions of any libraries that you are importing in your code sample.
Thanks @erictraut, I think anyone with VSCode and Python can replicate this:
Steps to replicate:
cd /tmp(example)mkdir temp_projectpython -m venv .venvsource .venv/bin/activatepip install djangodjango-admin startproject temp_projectcd temp_project- Launch VSCode and navigate to * /tmp/temp_project/temp_project * and open any of the py files or create a new one.
- Type in the following:
from django.contrib.auth.models import User - Start typing
User.objects.creaand you will notice inherited functions are not listed.
Thanks for the additional steps. Steps 9 and 10 provided the information I needed.
The problem is caused by the django type stubs that ship with pylance. They declare the type of objects to be BaseManager[Any], and the objects variable isn't redeclared (to a narrower type) in any other class within the User hierarchy. In the django source code, the AbstractUser class (which is one of the base classes of User) assigns an instance of UserManager to objects.
If I manually add the following line to the AbstractUser class declaration in the django/contrib/autho/models.pyi type stub, I then receive the expected completion suggestions.
objects: UserManager = ...

So this will need to be fixed in the type stubs bundled with pylance.
Thank you so much for this. So, do I have to make any changes my end? or the issue should be fixed in future releases?
Also, could you please explain how can I manually amend the pyi file? Just out of curiosity.
do I have to make any changes my end?
No, you don't need to make any changes. The problem is with one of the ".pyi" files bundled with pylance.
Also, could you please explain how can I manually amend the pyi file?
You can right-click on "User" and choose "Go To Declaration". This will open the file django/contrib/auth/models.pyi. Within the AbstractUser class definition, add the line objects: UserManager = .... Then save the file.
Thank you so much, much appreciated 🙏🏻🙏🏻.
Sorry one more thing, in my example, if I type in user followed by a dot, nothing shows up.
user = User.objects.create_user(username=user_name, password=password1, email=email, first_name=first_name, last_name=last_name)
user.
Ah, good catch. Upon closer inspection, UserManager is generic and accepts a single type argument. So the type argument User needs to be added after UserManager, as in objects: UserManager[User] = ....
Thanks a lot :) btw any recommended resources that easily explains the pyi jargon?
There's a bunch of documentation on the pyright project site. Pyright is the type checker that Pylance is built upon. Here's a good starting place.
Thanks Eric for all your help :) shall I close the issue or it needs to remain opened until the issue is fixed on Pylance?
Let's leave it open for now.
Looks like this has been fixed.