Skip to content

python type checking/ pylance / LSP support in cells with %%cell magic #13327

Closed Answered by Josverl
Josverl asked this question in Questions and Answers
Discussion options

You must be logged in to vote

for reference:
I was able to implement this functionality using an input transformer to remove/cleanup the # before Python gets at it.

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…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Josverl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant