Comment out IPython magics in Python virtual documents to avoid spurious diagnostics#1013
Merged
Merged
Conversation
Contributor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
seeM
approved these changes
Jun 17, 2026
| # | ||
| # | ||
| # | ||
| # |
Collaborator
There was a problem hiding this comment.
I'm not sure if this has any user-facing consequences: should these be commented out including the original code?
# %reload_ext pandas
# %%timeit
# !pip install pandas
# %time x = 1
Collaborator
Author
There was a problem hiding this comment.
I don't believe so, no. You can see how typical vdocs look here (empty comment lines):
https://github.com/quarto-dev/quarto/tree/main/apps/vscode/src/test/examples/generated_snapshots/vdoc
The extension keeps track of the mapping between the vdoc and the real doc, and the real doc is still there as well to get the info when needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #1011
IPython magic lines (
%,%%) and shell escapes (!) inside Python code cells are not valid Python, so third-party language servers like Pyrefly and Ruff flag them with spurious diagnostics:This does not happen in Jupyter notebooks, because tooling such as the
ruff_notebookcrate sanitizes notebook input first by commenting out these lines.Change in this PR
When Quarto builds the Python virtual document that gets fed to embedded language servers, magic and shell-escape lines are now replaced in place with the comment character (
#) instead of being copied verbatim. This mirrors the existing mechanism that already blanks out non-code lines:Because the lines are substituted (not removed), line counts stay identical and all the existing position-mapping logic (
adjustedLine/unadjustedLine) keeps working unchanged.Details:
commentMagicslanguage option, enabled for Python only since these are IPython/Jupyter specific./^\s*(%{1,2}|!)/(from the issue) matches line magics, cell magics, and shell escapes, including leading whitespace.injectdirectives (# type: ignore,# flake8: noqa), this applies to thediagnosticsaction too, which is the path the false diagnostics come through.vdoc/magics.qmd) exercising%,%%,!, an indented%time, and surrounding real Python, plus a snapshot test confirming the magic lines become#while real Python is preserved.%%bash, etc.) only comment out the magic line itself, not the rest of the cell body. This matches the issue's suggested regex and keeps the change simple; full cell-magic handling can be a follow-up if users report it.