Inside of a notebook, magic cells should not cause an error in pylance
See original GitHub issueAdd a cell like so:
%connect_info
That is a valid magic command. It would be nice if Pylance would recognize these.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:22 (10 by maintainers)
Top Results From Across the Web
Pylance "Invalid Character in token" for !pip commands
The code still runs fine when I press Run Cell. How can I make Pylance ignore these commands or get rid of the...
Read more >Working with Jupyter code cells in the Python Interactive window
To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Once the appropriate environment is activated, ...
Read more >Error fixed - "matplotlib.pyplot" not resolved from source. in ...
Hello Friends, In this tutorial session we will see the quick fix of the common error that comes when we work with Jupyter...
Read more >VS Code for python
pylance (source) is an extension that works alongside Python in Visual Studio Code to provide performant language support.
Read more >Built-in magic commands — IPython 8.7.0 documentation
You can create and register your own Magics with IPython. ... In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I wanted to try and make the case that this issue should be treated more like a bug.
I see how this can be considered an enhancement (it is a new scenario which was not considered initially), but for a jupyter notebook beginner like myself, a red squiggle in a notebook indicates that I did something wrong, and a squiggle on the first character of a notebook indicates that perhaps I have a setup issue. Despite the error, the cell actually executed without a problem. IMHO, in general, it is problematic if a tool reports a squiggle and I have no possibility to get rid of it; this erodes my trust in the tool. This can also be frustrating for users who want to have a clean Problems panel.
Happy to read that this issue is being addressed - the wiggles are really annoying. As far as I understand, ipykernel adds three syntax additions that are relevant and should be addressed here.
line magics, eg. %pip. Those are by definition just single lines and can probably be handled by simply replacing % against # when the % is at the beginning of the line. I guess this would be a good start already, although this approach is not 100% exact yet – depending on the options available here, it could be improved by testing if the %… token is part of a literal and/or by testing if the name of the magic is known. However, in case of checking against magics names, I would appreciate an algorithm which can cope with arbitrary magics and would not only test for some well-known magics names.
cell magics., eg. %%writefile. Cell magics pass the entire content of the cell to the corresponding magic. The content can be any language. My suggestion here would be to just ignore the entire cell if it starts with %%, and if it is parsed at all, it should be parsed by the language server that belongs to the language of the magic but not by default by a Python language server.
System shell access, eg. !ls. In principle the same as line magics, with the only difference that the content of the line is executed as a shell command if it starts with a !.
Sometimes, ipykernel also expands variables, ie. it replaces variable tokens like $my_var with corresponding variable values defined outside of the cell. However, Python cells seem not to do this variable expansion, hence can likely be neglected in this case.