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

Auto-import code actions do not work in notebooks

Open nfx opened this issue 4 years ago • 22 comments
trafficstars

In order to simplify development experience, i'd like to see so much used import hints in VSCode. image

nfx avatar Mar 07 '21 15:03 nfx

Thanks for the suggestion @nfx.

Could you describe your issue a bit deeper? Is Intellisense not giving you an option to import UNPROCESSED from the right place in your workspace?

DavidKutu avatar Mar 08 '21 18:03 DavidKutu

@DavidKutu nope, intellisense does not do that :( how to reproduce - create a Pipenv project, add click as dependency, pipenv sync, wait till everything is installed, create a python file, type print(UNPROCESSED), see red line highlight. And i'd expect a hint, like in IntelliJ (community edition + python plugin), to show me import suggestions.

nfx avatar Mar 09 '21 08:03 nfx

Some other questions @nfx. Is this on a python file or on a notebook?

Also, could you please paste your versions from Help -> About and the jupyter extension version you're using?

And finally, what do you have in the python.languageServer setting?

DavidKutu avatar Mar 09 '21 17:03 DavidKutu

@DavidKutu this one was in a python file. but i'd want to see it in a notebook as well.

i'm using insiders build (it broke last night!)

Extension version: 2021.3.600686576
VS Code version: Code - Insiders 1.54.0-insider (Universal) (e590188f17162393f50feec19263398e6fe02d13, 2021-02-26T20:39:12.763Z)
OS version: Darwin x64 19.6.0

A/B Experiments
vsliv695:30137379
vsins829:30139715
vsliv368:30146709
vsreu685:30147344
python383:30185418
pythonvspyt602:30263608
vspor879:30202332
vspor708:30202333
vspor363:30204092
vstry244:30244315
pythonvsdeb440:30224570
pythonvsded773:30223139
pythonvspyt875:30259475
pythonvspyt639:30264824
pythontb:30258533

nfx avatar Mar 10 '21 12:03 nfx

Pylance supports quick fixes for undefined variables of this nature. If you enable Pylance you should be able to try it out.

jakebailey avatar Mar 11 '21 21:03 jakebailey

@jakebailey what's the difference between pylance, mspython, etc etc?.. i'm confused.

nfx avatar Mar 11 '21 22:03 nfx

The Python extension supports multiple "language servers", which provide functionality such as diagnostics, completions, tooltips, etc. By default, the Python extension bundles and uses jedi.

To run pylance instead, you can follow the quickstart guide in our README: https://github.com/microsoft/pylance-release#quick-start

jakebailey avatar Mar 11 '21 22:03 jakebailey

@jakebailey okay, pylance is def better for normal editor, thanks for pointing to it, but it doesn't work for native Jupyter image image

nfx avatar Mar 11 '21 22:03 nfx

Are you using the old-style notebooks, or "native notebooks"? I'm not sure that the the old-style notebooks support quick fixes, but the newer native notebooks may.

jakebailey avatar Mar 11 '21 22:03 jakebailey

@jakebailey i use "native notebooks" when insiders build is not broken (50% of the time 😝 ). i think you see it from "+ Code" in the middle of the screen, new exception formatter and play button on the left and not on the right

nfx avatar Mar 12 '21 09:03 nfx

I see. It's very possible that their custom LSP -> notebook logic doesn't yet support quick fixes like these. They have to do a bit of massaging to the messages to make them apply to notebooks (and the notebook API may not even support them quite yet either, I'm unsure).

But, we're sort of unable to change anything in this area ourselves. We give quick fixes when requested, and it's up to the client (in this case, the Jupyter extension) to request and display them.

@rchiodo Do you know if quick fix support has been hooked up?

jakebailey avatar Mar 12 '21 19:03 jakebailey

I believe 'quick fixes' are code actions, right? These are not supported because they require the real file to exist. I believe this would require pylance to understand the notebook document. Otherwise it tries to apply the fix to our in memory file.

rchiodo avatar Mar 12 '21 19:03 rchiodo

Yes, code actions. We don't actually make file changes ourselves, there's a command that returns text edits the client is supposed to be applying, which shouldn't be different than a completion's text edits, right?

jakebailey avatar Mar 12 '21 19:03 jakebailey

If I remember correctly the code actions returned a command that then went back into the pylance extension. The pylance extension is of course confused at that point because the file doesn't really exist.

rchiodo avatar Mar 12 '21 19:03 rchiodo

The code action response contains commands which VS Code (or the client in general) runs on acceptance, but the commands themselves do not apply the edits directly to any file from my reading of the code. Seems like a case the middleware needs to rewrite.

I'll also point out that in the above screenshots, I'm not seeing the fixes appear at all, so they wouldn't even get to the place where the command could be run, no? It could be that VSC's notebooks aren't even sending the "give me the code actions for this location" call at all.

@nfx If you hit Ctrl+. on the squiggled part of the code, do you see a quick fix menu appear? Or is there no way to access them?

jakebailey avatar Mar 12 '21 19:03 jakebailey

Code actions are completely canceled in the notebook case. Because the commands when I had them there would just throw exceptions.

I don't think we can rewrite them too because we can't guess as to what to rewrite. A command has arbitrary parameters.

rchiodo avatar Mar 12 '21 21:03 rchiodo

The command contains paths, but the command is generated by Pylance and returned to Pylance only (nobody else), which then generates the text updates and sends those back as it does other text edits for completions. I wasn't suggesting that all commands in totality have their paths rewritten, just that the text edits we return be rewritten similarly to other positions/ranges for other calls.

I think we should try and not hide the code actions, and see what happens.

jakebailey avatar Mar 12 '21 21:03 jakebailey

@jakebailey didn't know about Ctrl+., probably it's cmd+. on mac... is it like alt+enter on intellij?

nfx avatar Mar 16 '21 19:03 nfx

code action and command should be disabled under notebook for now. and auto-import on completion should be disabled on notebook as well. we can only enable it once we can make those feature (code generation) notebook aware so that it doesn't generate code in invalid location

heejaechang avatar Jan 03 '22 22:01 heejaechang

@heejaechang you can simplify this by generating code in always-top cell - it's already more-or-less convention. same cell will be ideal.

nfx avatar Jan 04 '22 10:01 nfx

@nfx we are currently experimenting with multiple approaches to provide better notebook supports in vscode. once we have something concrete, we will let you know. thank you for using Pylance!

heejaechang avatar Jan 28 '22 19:01 heejaechang

@nfx, auto-import works for me after opting into the notebooks experiment that we shipped in Pylance 2022.5.3.

Give it a try:

  1. Install the latest builds of the Python, Pylance, and Jupyter extensions.
  2. Set "python.pylanceLspNotebooksEnabled": true
  3. Reload VS Code.

debonte avatar Jun 09 '22 22:06 debonte

We're closing this issue since it is fixed when python.pylanceLspNotebooksEnabled is set to true and as of Python extension version 2022.14.0 that is now the default.

If you are setting python.pylanceLspNotebooksEnabled to false, we would like to understand why. Please file an issue.

debonte avatar Sep 08 '22 05:09 debonte