Skip to content

Ruff triggered via jupyterlab-lsp -> python-lsp-server -> python-lsp-ruff chain doesn't recognize that it's checking a Jupyter notebook #1154

@sergey-protserov-uhn

Description

@sergey-protserov-uhn

Description

Hello!

I am not super sure that this is a bug in jupyterlab-lsp, because it may be a bug in python-lsp-server, its Ruff plugin (python-lsp-ruff) or Ruff itself. However, I thought that I will report it here, please let me know if the problem is actually downstream, and I will create another issue there.

Ruff has special handling of some linting rules when it detects that it's checking a Jupyter notebook and not a regular Python file: for example, useless-expression is not triggered for final top-level expression in a cell.

Indeed, when I run ruff check for a Jupyter notebook with such expressions, useless-expression is not triggered. However, it is triggered when I open such a notebook in JupyterLab, it is highlighted and displayed in a diagnostics window. I believe somewhere along the call chain the information that we are checking a notebook is lost, and Ruff interprets it as if it was a regular Python file.

I also get spurious unsorted-imports trigger for the first cell with imports, again only when viewing the notebook in JupyterLab, and not when checking it from command line. While this rule doesn't declare any special handling of notebooks on its page, this may be related.

Here is what I seen when I click on the LSP badge in the status bar:

Image

Thank you!

Context

  • Operating System and version: GNU/Linux, Fedora 42
  • Browser and version: Firefox 142.0 (64-bit)
  • jupyterlab version: 4.4.4 from conda-forge
  • jupyterlab-lsp version: 5.1.1 from conda-forge
  • python-lsp-server version: 1.12.2 from conda-forge
  • python-lsp-ruff version: 2.2.2 from conda-forge
  • ruff version: 0.12.1 from conda-forge
Required: installed server extensions
Config dir: /home/user_main/.jupyter

Config dir: /home/user_main/.miniforge3/envs/main/etc/jupyter
jupyter_lsp enabled
- Validating jupyter_lsp...
jupyter_lsp 2.2.5 OK
jupyter_server_mathjax enabled
- Validating jupyter_server_mathjax...
jupyter_server_mathjax OK
jupyter_server_terminals enabled
- Validating jupyter_server_terminals...
jupyter_server_terminals 0.5.3 OK
jupyterlab enabled
- Validating jupyterlab...
jupyterlab 4.4.4 OK
jupyterlab_git enabled
- Validating jupyterlab_git...
jupyterlab_git 0.51.2 OK
jupyterlab_myst enabled
- Validating jupyterlab_myst...
jupyterlab_myst 2.4.2 OK
nbdime enabled
- Validating nbdime...
nbdime 4.0.2 OK
notebook enabled
- Validating notebook...
notebook 7.4.3 OK
notebook_shim enabled
- Validating notebook_shim...
notebook_shim OK
panel.io.jupyter_server_extension enabled
- Validating panel.io.jupyter_server_extension...
Extension package panel.io.jupyter_server_extension took 0.5003s to import
panel.io.jupyter_server_extension OK

Config dir: /usr/local/etc/jupyter

Required: installed lab extensions
Config dir: /home/user_main/.jupyter

Config dir: /home/user_main/.miniforge3/envs/main/etc/jupyter
jupyter_lsp enabled
- Validating jupyter_lsp...
jupyter_lsp 2.2.5 OK
jupyter_server_mathjax enabled
- Validating jupyter_server_mathjax...
jupyter_server_mathjax OK
jupyter_server_terminals enabled
- Validating jupyter_server_terminals...
jupyter_server_terminals 0.5.3 OK
jupyterlab enabled
- Validating jupyterlab...
jupyterlab 4.4.4 OK
jupyterlab_git enabled
- Validating jupyterlab_git...
jupyterlab_git 0.51.2 OK
jupyterlab_myst enabled
- Validating jupyterlab_myst...
jupyterlab_myst 2.4.2 OK
nbdime enabled
- Validating nbdime...
nbdime 4.0.2 OK
notebook enabled
- Validating notebook...
notebook 7.4.3 OK
notebook_shim enabled
- Validating notebook_shim...
notebook_shim OK
panel.io.jupyter_server_extension enabled
- Validating panel.io.jupyter_server_extension...
panel.io.jupyter_server_extension OK

Config dir: /usr/local/etc/jupyter

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions