5

I'm playing around with Python's and NumPy's methods to compare their performance:

import numpy as np

massive_array = np.random.random(100000)

%timeit sum(massive_array) # Python's sum()
%timeit np.sum(massive_array) # NumPy's np.sum()

Which works fine and returns the following:

9.56 ms ± 523 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
51.9 µs ± 1.59 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

The only problem is that, while using the notebook on vscode, I get an error flagged by Pylance regarding the percentage symbol for %timeit:

enter image description here

What's the reason for it to be flagged? Shouldn't that be a valid expression, given that it also executed successfully?

starball
  • 20,030
  • 7
  • 43
  • 238
ale917k
  • 1,494
  • 7
  • 18
  • 37
  • 1
    That's not valid Python syntax. That's an IPython line magic. This raises the question, why do you have Pylance checking for valid Python syntax in a Jupyter notebook that accepts input that isn't valid Python syntax? – user2357112 Mar 20 '22 at 11:40
  • 1
    Great point! After your comment I tested without the extension, but intellisense seems to be quite degraded; Do you perhaps know if it's possible to disable the warnings while keeping Pylance intellisense? – ale917k Mar 20 '22 at 14:48

1 Answers1

1

After reading through some GitHub issue tickes, if I understand correctly, this shouldn't be an issue anymore in newer versions of the Jupyter and Python extensions for VS Code.

See Inside of a notebook, magic cells should not cause an error in pylance #17058 (in microsoft/vscode-python), which was fixed by the following two pull requests:

All that confuses me now is why this problem happened to you, given that you asked this question on 2022-03-20, and those changes were made around 2021-08-20.

Note: It doesn't look like it from your screenshot, but in case you're actually using a cell script (.py file with #%% markers) and not a Jupyter notebook, then edit your user or workspace settings.json file to set "jupyter.magicCommandsAsComments": true, which allows you to use magic commands in comments (see #3263 and #6494).

For your learning purposes / reference, I found this info by googling "github vscode jupyter issues syntax error problem for ipython magic" and looking through the top search results and linked GitHub issue tickets.

starball
  • 20,030
  • 7
  • 43
  • 238