pylance-release
pylance-release copied to clipboard
Assign to variable from commented-out magic command
IPython allows expanding Python variables into magic shell commands and also assigning the command's stdout to a variable:
# %%
num = 42
stdout = !echo {num} # raises: Pylance: Expected expression
print(stdout)
>>> ['42']
But it seems there's no way to do this without syntax error.
https://github.com/microsoft/vscode-jupyter/issues/3263 introduced "jupyter.magicCommandsAsComments": true which allows to do:
slurm_cmd = """
sbatch --partition icelake --account=SL3-CPU --time 1 --nodes 1 --wrap "python -c 'print(42)'"
"""
slurm_msg = ""
#! slurm_msg = {slurm_cmd}
print(f"{slurm_msg=} at {datetime.now():%Y-%m-%d %H-%M}")
>>> slurm_msg='' at 2022-04-24 20-04
The shell command runs as expected but the variable assignment is lost. Any chance magicCommandsAsComments could get a feature upgrade?
@janosh looks like there are two issues here. One is the syntax error and the other is variable value being lost. Is that correct?
Transferring to lsp-middle-ware to ensure statements such as stdout = !echo {num} are not flagged as errors.
@DonJayamanne That's correct, 2 issues here, though the syntax error can be worked around as in the 2nd code snippet with this clunky construction.
slurm_msg = ""
#! slurm_msg = {slurm_cmd}
So the only real issue is the variable assignment being lost. But if there's a proper solution to the syntax error (maybe marking it as acceptable code in pylance), this could fix the lost assignment as well and in a cleaner way.
The middleware layer is being deprecated (and well never handled this sort of thing anyway). Might be something to do in pylance itself.
there seems an issue with ipython mode
I ran into a similar problem