python type checking/ pylance / LSP support in cells with %%cell
magic
#13327
-
I notice that as soon as I add any %%cell magic , that this shuts down the static typechecking and linting while for just python this is not an issue , it does cause a problem for some custom magics that I am working on for MicroPython - a dialect of python. is there a way that the cell magic can indicate that type checking and linting should not be stopped / aborted ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Possibly related : Assign to variable from commented-out magic command if I read it correctly this should allow for
so I would expect a cell #!%%micropython
from machine import Pin
led=Pin(25,Pin.OUT)
led(1) |
Beta Was this translation helpful? Give feedback.
-
for reference: def load_ipython_extension(ipython: InteractiveShell):
# register the input transformer to allow %%cell_magics in comments
ipython.input_transformers_cleanup.append(comment_magic_transformer)
# ...
import re
re_comment_magic = r"#[ |\t|!]*%%((micropython|python|mypy|script))"
# Regex to match a comment magic, e.g. `# %%micropython
subst = r"\g<1>"
# replace with all but the comment & whitespace
def comment_magic_transformer(lines: list[str]):
"""
Transforms a cell with a commented cell magic into a cell without the comment, but with the %%celmagic.
`# %%micropython` --> `%%micropython`
This allows Pylance to only see python code and not the magic,
which would otherwise confuse it, and cause it to be disabled.
"""
if "%%" not in lines[0]:
return lines
return [re.sub(re_comment_magic, subst, lines[0])] + lines[1:] This depends-on the fact that Pylance is (currently) not smart enough to do the same , and also fixes the same problem for the %%pythonX magics |
Beta Was this translation helpful? Give feedback.
for reference:
I was able to implement this functionality using an input transformer to remove/cleanup the
#
before Python gets at it.