pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Autocomplete not working properly for inherited classes - Ubuntu

Open 0xab3d opened this issue 3 years ago • 11 comments

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. Image 0

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

Image 1

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

Image 2

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.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, 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

0xab3d avatar May 27 '22 15:05 0xab3d

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.

erictraut avatar May 29 '22 06:05 erictraut

Thanks @erictraut, I think anyone with VSCode and Python can replicate this:

Steps to replicate:

  1. cd /tmp (example)
  2. mkdir temp_project
  3. python -m venv .venv
  4. source .venv/bin/activate
  5. pip install django
  6. django-admin startproject temp_project
  7. cd temp_project
  8. Launch VSCode and navigate to * /tmp/temp_project/temp_project * and open any of the py files or create a new one.
  9. Type in the following: from django.contrib.auth.models import User
  10. Start typing User.objects.crea and you will notice inherited functions are not listed.

0xab3d avatar May 30 '22 08:05 0xab3d

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 = ...

Screen Shot 2022-05-30 at 10 24 31 AM

So this will need to be fixed in the type stubs bundled with pylance.

erictraut avatar May 30 '22 17:05 erictraut

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.

0xab3d avatar May 31 '22 07:05 0xab3d

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.

erictraut avatar May 31 '22 07:05 erictraut

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.

0xab3d avatar May 31 '22 07:05 0xab3d

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] = ....

erictraut avatar May 31 '22 15:05 erictraut

Thanks a lot :) btw any recommended resources that easily explains the pyi jargon?

0xab3d avatar May 31 '22 20:05 0xab3d

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.

erictraut avatar May 31 '22 20:05 erictraut

Thanks Eric for all your help :) shall I close the issue or it needs to remain opened until the issue is fixed on Pylance?

0xab3d avatar Jun 02 '22 06:06 0xab3d

Let's leave it open for now.

erictraut avatar Jun 02 '22 06:06 erictraut

Looks like this has been fixed.

rchiodo avatar Oct 14 '22 23:10 rchiodo