From b6b12b6a905b614730b699356e1f9119139ec0f2 Mon Sep 17 00:00:00 2001 From: Felix Williams Date: Fri, 10 Nov 2023 10:43:25 +0000 Subject: [PATCH 1/3] add support for ruff extension option --- pylsp_ruff/plugin.py | 5 +++++ pylsp_ruff/settings.py | 2 ++ pyproject.toml | 2 +- tests/test_ruff_lint.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index a36babf..94972b4 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -556,6 +556,11 @@ def build_arguments( if not PurePath(document_path).match(path): continue args.append(f"--ignore={','.join(errors)}") + if settings.extension: + extension_pairs = ",".join( + f"{ext}:{lang}" for ext, lang in settings.extension.items() + ) + args.append(f"--extension={extension_pairs}") if extra_arguments: args.extend(extra_arguments) diff --git a/pylsp_ruff/settings.py b/pylsp_ruff/settings.py index 0ccda7e..59fecd0 100644 --- a/pylsp_ruff/settings.py +++ b/pylsp_ruff/settings.py @@ -27,6 +27,8 @@ class PluginSettings: severities: Optional[Dict[str, str]] = None + extension: Optional[Dict[str, str]] = None + def to_camel_case(snake_str: str) -> str: components = snake_str.split("_") diff --git a/pyproject.toml b/pyproject.toml index cfdf720..867c621 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ readme = "README.md" requires-python = ">=3.7" license = {text = "MIT"} dependencies = [ - "ruff>=0.1.0, <0.2.0", + "ruff>=0.1.5, <0.2.0", "python-lsp-server", "lsprotocol>=2022.0.0a1", "tomli>=1.1.0; python_version < '3.11'", diff --git a/tests/test_ruff_lint.py b/tests/test_ruff_lint.py index 41026fb..b456a5b 100644 --- a/tests/test_ruff_lint.py +++ b/tests/test_ruff_lint.py @@ -85,6 +85,7 @@ def test_ruff_config_param(workspace): "config": ruff_conf, "extendSelect": ["D", "F"], "extendIgnore": ["E"], + "extension": {"ipynb": "python"}, } } } @@ -96,6 +97,7 @@ def test_ruff_config_param(workspace): assert f"--config={ruff_conf}" in call_args assert "--extend-select=D,F" in call_args assert "--extend-ignore=E" in call_args + assert "--extension=ipynb:python" in call_args def test_ruff_executable_param(workspace): @@ -242,3 +244,37 @@ def f(): assert diag["code"] != "F401" os.unlink(os.path.join(workspace.root_path, "pyproject.toml")) + + +def test_notebook_input(workspace): + doc_str = r""" +print('hi') +import os +def f(): + a = 2 +""" + # attribute the python code to a notebook file name per jupyterlab-lsp + doc_uri = uris.from_fs_path(os.path.join(workspace.root_path, "Untitled.ipynb")) + workspace.put_document(doc_uri, doc_str) + doc = workspace.get_document(doc_uri) + + diags = ruff_lint.pylsp_lint(workspace, doc) + # without the extension option, we get a syntax error because ruff expects JSON + assert len(diags) + assert diags[0]["code"] == "E999" + + workspace._config.update( + { + "plugins": { + "ruff": { + "extension": {"ipynb": "python"}, + } + } + } + ) + diags = ruff_lint.pylsp_lint(workspace, doc) + diag_codes = [diag["code"] for diag in diags] + assert "E999" not in diag_codes + assert "E402" in diag_codes + assert "F401" in diag_codes + assert "F841" in diag_codes From 73598f813306bf137d2e29aa289c2d8221156f64 Mon Sep 17 00:00:00 2001 From: Felix Williams Date: Fri, 10 Nov 2023 13:59:13 +0000 Subject: [PATCH 2/3] add `extension` to list of config keys in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 71f02ea..713ca97 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ the valid configuration keys: - `pylsp.plugins.ruff.format`: List of error codes to fix during formatting. The default is `["I"]`, any additional codes are appended to this list. - `pylsp.plugins.ruff.unsafeFixes`: boolean that enables/disables fixes that are marked "unsafe" by `ruff`. `false` by default. - `pylsp.plugins.ruff.severities`: Dictionary of custom severity levels for specific codes, see [below](#custom-severities). + - `pylsp.plugins.ruff.extension`: Dictionary of file extensions to file type e.g. `{"ipynb":"python"}`. Used to override ruff's own inference. For more information on the configuration visit [Ruff's homepage](https://beta.ruff.rs/docs/configuration/). From 5c6a9593216cfbbaf57fc266304183e59b72cbf5 Mon Sep 17 00:00:00 2001 From: Felix Williams Date: Tue, 14 Nov 2023 10:50:30 +0000 Subject: [PATCH 3/3] remove option to add `--extension` --- README.md | 1 - pylsp_ruff/plugin.py | 6 +----- pylsp_ruff/settings.py | 2 -- tests/test_ruff_lint.py | 17 +---------------- 4 files changed, 2 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 713ca97..71f02ea 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,6 @@ the valid configuration keys: - `pylsp.plugins.ruff.format`: List of error codes to fix during formatting. The default is `["I"]`, any additional codes are appended to this list. - `pylsp.plugins.ruff.unsafeFixes`: boolean that enables/disables fixes that are marked "unsafe" by `ruff`. `false` by default. - `pylsp.plugins.ruff.severities`: Dictionary of custom severity levels for specific codes, see [below](#custom-severities). - - `pylsp.plugins.ruff.extension`: Dictionary of file extensions to file type e.g. `{"ipynb":"python"}`. Used to override ruff's own inference. For more information on the configuration visit [Ruff's homepage](https://beta.ruff.rs/docs/configuration/). diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 94972b4..590ff49 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -516,6 +516,7 @@ def build_arguments( args.append("--exit-zero") # Use the json formatting for easier evaluation args.append("--output-format=json") + args.append("--extension=ipynb:python") if fix: args.append("--fix") else: @@ -556,11 +557,6 @@ def build_arguments( if not PurePath(document_path).match(path): continue args.append(f"--ignore={','.join(errors)}") - if settings.extension: - extension_pairs = ",".join( - f"{ext}:{lang}" for ext, lang in settings.extension.items() - ) - args.append(f"--extension={extension_pairs}") if extra_arguments: args.extend(extra_arguments) diff --git a/pylsp_ruff/settings.py b/pylsp_ruff/settings.py index 59fecd0..0ccda7e 100644 --- a/pylsp_ruff/settings.py +++ b/pylsp_ruff/settings.py @@ -27,8 +27,6 @@ class PluginSettings: severities: Optional[Dict[str, str]] = None - extension: Optional[Dict[str, str]] = None - def to_camel_case(snake_str: str) -> str: components = snake_str.split("_") diff --git a/tests/test_ruff_lint.py b/tests/test_ruff_lint.py index b456a5b..c56a1ac 100644 --- a/tests/test_ruff_lint.py +++ b/tests/test_ruff_lint.py @@ -85,7 +85,6 @@ def test_ruff_config_param(workspace): "config": ruff_conf, "extendSelect": ["D", "F"], "extendIgnore": ["E"], - "extension": {"ipynb": "python"}, } } } @@ -97,7 +96,6 @@ def test_ruff_config_param(workspace): assert f"--config={ruff_conf}" in call_args assert "--extend-select=D,F" in call_args assert "--extend-ignore=E" in call_args - assert "--extension=ipynb:python" in call_args def test_ruff_executable_param(workspace): @@ -183,6 +181,7 @@ def f(): "--quiet", "--exit-zero", "--output-format=json", + "--extension=ipynb:python", "--no-fix", "--force-exclude", f"--stdin-filename={os.path.join(workspace.root_path, '__init__.py')}", @@ -258,20 +257,6 @@ def f(): workspace.put_document(doc_uri, doc_str) doc = workspace.get_document(doc_uri) - diags = ruff_lint.pylsp_lint(workspace, doc) - # without the extension option, we get a syntax error because ruff expects JSON - assert len(diags) - assert diags[0]["code"] == "E999" - - workspace._config.update( - { - "plugins": { - "ruff": { - "extension": {"ipynb": "python"}, - } - } - } - ) diags = ruff_lint.pylsp_lint(workspace, doc) diag_codes = [diag["code"] for diag in diags] assert "E999" not in diag_codes