Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] How to use cell magic like "%%time", "!ls" without pyright complaining? #291

Closed
kanghengliu opened this issue Mar 2, 2025 · 6 comments

Comments

@kanghengliu
Copy link

First of all big thank you for this wonderful plugin. I'm trying to use cell magic like so:

Image

They would execute perfectly fine via ipython, but pyright complains about the syntax. Is there a way to suppress the error message?

On a side note that may be related, I'm not sure which plugin is causing this auto indent behavior after writing a cell magic:

Screen.Recording.mov
@benlubas
Copy link
Owner

benlubas commented Mar 2, 2025

You just won't be able to use pyright like this. It can't interpret ipython magics.

You could try to suppress specific warnings in specific cases. For example, I add this filter to suppress unused variable warnings when the variable starts with _: https://github.com/benlubas/.dotfiles/blob/a3eed454e0116740eee2c7863687c1b0ca7326c0/nvim/lua/benlubas/lsp_handlers.lua#L30

But I think you will still run into issues with unexpected expression warnings, or pyright failing to parse beyond the cell magic syntax error.

The indentation is very likely handled by treesitter, and it's just doing it's best to recover from invalid syntax. If you wanted to fix this, you could either:

  • disable indent in python entirely, just keep the same indent level when you hit enter (auto indent in python almost always just does this anyway, there are very few cases when it does other stuff such as inside multiline list/dict definitions)
  • try to write your own TS queries for indentation (or rather, alter the existing ones)
  • disable TS indents for python, and write/modify vim script indentation logic for python

@kanghengliu
Copy link
Author

@benlubas Thank you for the timely reply!

I tried messing around with possible solutions, also in addition explored code formatting options with conform.nvim:

Linting

  1. 🩹 method: add # pyright:ignore manually to every magic commands. Won't apply to other linters obviously
  2. The patching method you mentioned. Parsing is a problem but i think i can shamefully steal some parsing logic from other repos. nbQA has a set of tools to interacting with notebooks.
  3. nbQA does have support for linting with cell magic, though i've yet to explore.

Formatting

  1. conform.nvim has inject option for formatting code cells in the markdown filetype. It works very well, except for those cells with magic commands. Most of the python formatter will refuse to format.
  2. black has support for ipynb files, and md files converted using jupytext. However, while we can use black to format the entire notebook, we possibly lose the ability to format other languages, like R or json
  3. Could hack some format logic with --line-ranges option in black to escape the magic commands. However parsing is still an issue.

@benlubas
Copy link
Owner

benlubas commented Mar 2, 2025

nbQA is a really interesting project, thanks for sharing it! It does seem focused on linting and formatting though, and it doesn't have support for pyright. assuming that it can output to JSON or some other machine readable format, you could use it with a linter of your choice + nvim-lint

I don't see why nbQA would be restricted to python tooling. I'd imagine it could be easily adapted to other tools if it's not already.

As far as getting pyright to behave: Another approach that you could take is modifying otter.nvim. It's the plugin that sends code cell info to the LSP and syncs diagnostics and all that. Otter already has some mechanism for leaving out parts of syntax injected regions. You might be able to add a some type of filter for cell magics. this way otter will "hide" the cell magic from the language server/formatter/linter.

@kanghengliu
Copy link
Author

kanghengliu commented Mar 2, 2025

Modifying otter.nvim is actually a better idea. Thanks! I noticed this PR that recently got merged. It adds a preamble for cargo to each of the otter buffer, so linting could work. I wonder if the reverse (removing cell magic in our use case) could be done. I've added an issue to otter repo that describes this method.

@kanghengliu
Copy link
Author

Created a PR in otter.nvim to allow it to not sending matched patterns to LSP. This addresses the linting issue. However, formatting cells with cell magic will still need investigation with conform.nvim.

@kanghengliu
Copy link
Author

With the otter.nvim PR previously merged this thread is closed. For formatting please look at my feature request above at conform.nvim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants