Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intellisense not working on this particular python code snippet #1890

Closed
miko989 opened this issue Sep 30, 2021 · 8 comments
Closed

Intellisense not working on this particular python code snippet #1890

miko989 opened this issue Sep 30, 2021 · 8 comments
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs stub

Comments

@miko989
Copy link

miko989 commented Sep 30, 2021

Issue Type: Bug

Intellisense for python is normally working for me without any issue. However, the following code makes it unresponsive. In particular, as soon as I open the file containing this code in VScode, the Intellisense stops working altogether until I restart VScode. I have not been able to find any error message corresponding to opening of this particular file. Can you spot the issue?

#!/usr/bin/env python3

import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit

def gaus(x,a,x0,sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))

nbins = 50
xmax = 30
noise = np.random.normal(10, 1, 1000)
plt.figure("noise")
hist,bin_edges,_ = plt.hist(noise,range=(0,xmax), bins=nbins, \
    label=f"Mean: {np.mean(noise):5.1f} e$^-$\nRMS:  {np.std(noise):5.1f} e$^-$")
bin_mid = (bin_edges[:-1] + bin_edges[1:])/2
popt,pcov = curve_fit(gaus, bin_mid, hist, [10,np.mean(noise),np.std(noise)])
plt.plot(np.arange(0,xmax,xmax/nbins/10),gaus(np.arange(0,xmax,xmax/nbins/10),*popt), \
    label=f'$\mu$:    {popt[1]:5.1f} e$^-$\n$\sigma$:    {popt[2]:5.1f} e$^-$')
plt.legend(loc="upper right", prop={"family":"monospace"})
plt.xlim(0,xmax)
plt.show()

VS Code version: Code 1.60.2 (Universal) (7f6ab5485bbc008386c4386d08766667e155244e, 2021-09-22T11:59:27.195Z)
OS version: Darwin x64 20.6.0
Restricted Mode: No

System Info
Item Value
CPUs Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz (8 x 2300)
GPU Status 2d_canvas: enabled
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
oop_rasterization: enabled
opengl: enabled_on
rasterization: enabled
skia_renderer: disabled_off_ok
video_decode: enabled
webgl: enabled
webgl2: enabled
Load (avg) 3, 3, 3
Memory (System) 32.00GB (8.63GB free)
Process Argv --crash-reporter-id 1c6b1f52-1a3f-4595-b623-b2ae03f5da42
Screen Reader no
VM 0%
Extensions (7)
Extension Author (truncated) Version
python ms- 2021.9.1246542782
vscode-pylance ms- 2021.9.3
jupyter ms- 2021.8.2041215044
jupyter-keymap ms- 1.0.0
remote-ssh ms- 0.65.8
remote-ssh-edit ms- 0.65.8
cpptools ms- 1.6.0
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
pythonvspyt602:30300191
vspor879:30202332
vspor708:30202333
vspor363:30204092
pythonvspyt639:30300192
pythontb:30283811
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
pythondataviewer:30285071
pythonvsuse255:30340121
vscod805cf:30301675
pythonvspyt200:30340761
binariesv615:30325510
vsccppwt:30364497
pythonvssor306:30344512
bridge0708:30335490
pygetstartedt2:30371810
dockerwalkthrucf:30370837
bridge0723:30353136
pythonrunftest32:30373476
pythonf5test824:30373475
javagetstartedc:30364665
pythonvspyt187:30373474
pydsgst2:30361792
vsqsis300:30374749

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Sep 30, 2021
@erictraut
Copy link
Contributor

It looks like the scipy stubs that we ship with pylance are missing the curve_fit symbol, so we're falling back on type inference to try to determine its type. This is taking a very long time given the complexity of the code.

@jakebailey
Copy link
Member

The stubs we ship are for the compiled code only, so this must be in a regular py file.

@erictraut
Copy link
Contributor

This is potentially related to microsoft/pyright#2324. The code in scipy is unannotated, and it involves a lot of complex nested loops, so it can be very costly to analyze.

@miko989
Copy link
Author

miko989 commented Oct 1, 2021

So what would be to solution? And in the meantime the mitigation?

@erictraut
Copy link
Contributor

I've added some heuristics to the type analyzer to skip function return type inference for complex functions like curve_fit that have no parameter annotations. This avoids the extremely long analysis times for these functions, but it also means that you will not get completion suggestions for the return result of these functions because the type will be unknown.

While you're waiting for this fix in next release of pylance (which is typically published on Wednesday), you can disable the python.analysis.useLibraryCodeForTypes. This will avoid the long analysis times, but it will also mean that completion suggestions will not be available for symbols that come from untyped libraries.

@miko989
Copy link
Author

miko989 commented Oct 4, 2021

Thanks a lot!

@jakebailey
Copy link
Member

I'm going to mark this one as fixed, as the main issue was the responsiveness. Getting the types right will take changes upstream or stubs to be written.

@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Oct 4, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.10.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2021100-6-october-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version needs stub
Projects
None yet
Development

No branches or pull requests

4 participants