-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Break in compatibility with jupyterlab-lsp + python-lsp-ruff since 0.0.285 #6847
Comments
Hey, thanks for opening the issue! First of all, your observation is spot on in that the You can see the URI for the document which has an extension of A few solutions which come to my mind:
Feel free to share any other suggestions :) Footnotes |
Hi all. Sorry for posting on an old issue. For me, I would prefer option 2: a new CLI flag, so that options such as per-file-ignores can work both in a jupyter lsp context as well as when running ruff on notebooks in command lines, pre-commit etc. An example of a per-file-ignore that can definitely be scoped to ipynb files is B018. |
Could our notebook parser (or a step upstream of it) just be updated to wrap the content in a single cell if it's not already? I don't see a |
I think that could work. My concern was that since ruff output includes cell numbers, it would interfere with the part of jupyterlab-lsp which maps line numbers in the linter output back to the original cells. |
I think the exclusion of cell numbers is a bug in the JSON format is a bug (see, e.g., #7664). Broadly, I'm worried that treating Python source as Jupyter could cause other problems, some unanticipated (e.g., if you accepted the code via Without doing deeper research, my current preferences would be something like:
|
Agree that 1. would be preferable. 2. is fine with me if we hide it from the CLI help menu until there are more user-focused use-cases. |
Thanks everyone! I thought that it might be too complicated to get python-lsp-ruff/jupyterlab-lsp to use linters that work on notebooks directly, so I made a PR implementing the |
My concern with 2 is that, compared to Prettier, we don't support different parsers. I prefer a CLI flag that is more high level and instead allows overriding the file type (mime type remapping), which can be useful in other use cases other than the one mentioned here. |
Can you spell out in a bit more detail what this CLI flag would look like and/or how it would differ from the above? |
Wouldn't that be in conflict with the inferred filetype from the filename's extension provided via FWIW, in |
What I have in mind is that you can map extensions to other files. I don't have a good python example but JSON supports different standards and a user may decide that all their We could expose the same option on the CLI and it could then be used as |
Interesting, so |
I kind of like that API |
Hi, some context from
The last issue about loss of structure could be alleviated if Jupyter ends up formalising a canonical text-based representation of notebooks, currently being discussed from the Markdown-first perspective in jupyter/enhancement-proposals#103 and even more if it includes a way to preserve cell-level metadata, but this has less of an impact on a discussion herein, maybe beyond naming of a future option (
|
So, the mapping will be from file extension to the language (or file extension
Yes, we're doing this in astral-sh/ruff-lsp#264 where the LSP creates a JSON text representing the Notebook in VSCode which is what is being sent to the Ruff CLI. This works for us as we're in control of what fields we use from the JSON format and I agree that it might be difficult to incorporate this in a general way for multiple tools. |
I've had a go at implementing this idea as a hidden flag. Is it OK if I raise a PR for it and close the previous PR? |
I'm not exactly sure if this is going to be enough. Ruff keeps track of the cell boundaries which are used to separate import blocks in each cell. So, for example, take the following two cells: Cell 1: from math import pi
import os Cell 2: from pathlib import Path
import sys Correct me if I'm wrong but the concatenated source code which Ruff would receive through Jupyterlab LSP would be: from math import pi
import os
from pathlib import Path
import sys The imports would be sorted as: import os
import sys
from math import pi
from pathlib import Path And then mapped back to the Notebook as: Cell 1: import os
import sys Cell 2: from math import pi
from pathlib import Path Notice that the imports have been moved between cells. Ruff's native implementation wouldn't do that and only sort imports in each cell. |
That's interesting. Are there any rules other than |
@felix-cw - |
Oh, interesting! Thanks for letting us know. So, it seems that they don't support code actions / formatting which are two main ways that we would edit a Notebook content. But, they do plan to support them in the future jupyter-lsp/jupyterlab-lsp#985.
I'm not exactly sure. My main concern is that I don't know how Jupyterlab LSP maps the location information between the actual Notebook source and the concatenated source. This would probably be updated when they plan to integrate editing cell content via code actions / formatting. There does exists a documentation around their source map model (https://github.com/jupyter-lsp/jupyterlab-lsp/blob/main/docs/Architecture.ipynb) which seems to be mapping the (line, character) value between multiple types of document and concatenating the source code with 2 blank lines? We could, theoretically, support diagnostics using any of the possible solutions mentioned in this issue but it might probably break when they add support for code actions. |
I think you do not need to worry about how jupyterlab-lsp handles mapping between cells and text content, I am sure this will work well. And yes there is a branch with code actions support but I cannot test it against ruff yet because it just rejects text content on the basis of extension ;) |
…le extension (#8373) ## Summary This PR addresses the incompatibility with `jupyterlab-lsp` + `python-lsp-ruff` arising from the inference of source type from file extension, raised in #6847. In particular it follows the suggestion in #6847 (comment) to specify a mapping from file extension to source type. The source types are - python - pyi - ipynb Usage: ```sh ruff check --no-cache --stdin-filename Untitled.ipynb --extension ipynb:python ``` Unlike the original suggestion, `:` instead of `=` is used to associate file extensions to language since that is what is used with `--per-file-ignores` which is an existing option that accepts a mapping. ## Test Plan 2 tests added to `integration_test.rs` to ensure the override works as expected --------- Co-authored-by: Charlie Marsh <[email protected]>
Thanks for merging my pull request! I think we need to add |
This is resolved when using https://github.com/python-lsp/python-lsp-ruff/releases/tag/v2.0.0 |
I'm not 100% sure this is the right project to raise this, since the problem comes from interaction with
python-lsp-ruff
andjuptyerlab-lsp
. I only raised it here since the issue arose from a change in ruff.Since 0.0.285, the jupyterlab-lsp plugin now gives the following diagnostic message:
I believe this is because jupyterlab-lsp extracts the python code from the notebook cells, then python-lsp-ruff passes it to ruff via stdin, and specifies
--stdin-filename
. Since #6628, this makes ruff expect ipynb JSON, rather than python code, which causes the error.The text was updated successfully, but these errors were encountered: