Skip to content

Commit fc2851a

Browse files
authored
Use ruff as linter and code formatter (#502)
1 parent cb213e0 commit fc2851a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+241
-213
lines changed

Diff for: .github/workflows/static.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@ jobs:
3838
# If we don't install pycodestyle, pylint will throw an unused-argument error in pylsp/plugins/pycodestyle_lint.py:72
3939
# This error cannot be resolved by adding a pylint: disable=unused-argument comment ...
4040
- run: |
41-
pip install -e .[pylint,pycodestyle,pyflakes]
42-
pip install black
43-
- name: Pylint checks
44-
run: pylint pylsp test
45-
- name: Code style checks with black
46-
run: black --check pylsp test
47-
- name: Pyflakes checks
48-
run: pyflakes pylsp test
41+
pip install -e .[pylint,pycodestyle]
42+
pip install ruff
43+
- name: ruff linter and code style checks
44+
run: ruff check pylsp test
45+
- name: ruff code formatter check
46+
run: ruff format --check pylsp test
4947
- name: Validate JSON schema
5048
run: echo {} | jsonschema pylsp/config/schema.json
5149
- name: Ensure JSON schema and Markdown docs are in sync

Diff for: .pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ reports = no
2828

2929
generated-members =
3030
pylsp_*
31-
cache_clear
31+
cache_clear

Diff for: MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ include README.md
22
include versioneer.py
33
include pylsp/_version.py
44
include LICENSE
5+
include ruff.toml
56
include .pylintrc
67
recursive-include test *.py

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ To run the test suite:
185185
pytest
186186
```
187187

188+
Running ruff as a linter and code formatter on the repo:
189+
```sh
190+
ruff check . # linter
191+
ruff check --fix . # fix all auto-fixable lint issues
192+
ruff format . # format the document
193+
```
194+
188195
After adding configuration options to `schema.json`, refresh the `CONFIGURATION.md` file with
189196

190197
```

Diff for: pylsp/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import os
5+
56
import pluggy
7+
68
from . import _version
79
from ._version import __version__
810

Diff for: pylsp/__main__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99

1010
try:
1111
import ujson as json
12-
except Exception: # pylint: disable=broad-except
12+
except Exception:
1313
import json
1414

15+
from ._version import __version__
1516
from .python_lsp import (
1617
PythonLSPServer,
1718
start_io_lang_server,
1819
start_tcp_lang_server,
1920
start_ws_lang_server,
2021
)
21-
from ._version import __version__
2222

2323
LOG_FORMAT = "%(asctime)s {0} - %(levelname)s - %(name)s - %(message)s".format(
2424
time.localtime().tm_zone

Diff for: pylsp/_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def throttle(seconds=1):
6161

6262
def decorator(func):
6363
@functools.wraps(func)
64-
def wrapper(*args, **kwargs): # pylint: disable=inconsistent-return-statements
64+
def wrapper(*args, **kwargs):
6565
if not hasattr(wrapper, "last_call"):
6666
wrapper.last_call = 0
6767
if time.time() - wrapper.last_call >= seconds:

Diff for: pylsp/config/config.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
3-
# pylint: disable=import-outside-toplevel
43

54
import logging
65
import sys
@@ -10,7 +9,7 @@
109
import pluggy
1110
from pluggy._hooks import HookImpl
1211

13-
from pylsp import _utils, hookspecs, uris, PYLSP
12+
from pylsp import PYLSP, _utils, hookspecs, uris
1413

1514
# See compatibility note on `group` keyword:
1615
# https://docs.python.org/3/library/importlib.metadata.html#entry-points
@@ -38,7 +37,7 @@ def _hookexec(
3837
# enable_tracing will set its own wrapping function at self._inner_hookexec
3938
try:
4039
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
41-
except Exception as e: # pylint: disable=broad-except
40+
except Exception as e:
4241
log.warning(f"Failed to load hook {hook_name}: {e}", exc_info=True)
4342
return []
4443

@@ -79,7 +78,7 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
7978
for entry_point in entry_points(group=PYLSP):
8079
try:
8180
entry_point.load()
82-
except Exception as e: # pylint: disable=broad-except
81+
except Exception as e:
8382
log.info(
8483
"Failed to load %s entry point '%s': %s", PYLSP, entry_point.name, e
8584
)

Diff for: pylsp/config/flake8_conf.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import logging
55
import os
6+
67
from pylsp._utils import find_parents
8+
79
from .source import ConfigSource
810

911
log = logging.getLogger(__name__)

Diff for: pylsp/config/pycodestyle_conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import pycodestyle
5+
56
from pylsp._utils import find_parents
6-
from .source import ConfigSource
77

8+
from .source import ConfigSource
89

910
CONFIG_KEY = "pycodestyle"
1011
USER_CONFIGS = [pycodestyle.USER_CONFIG] if pycodestyle.USER_CONFIG else []

Diff for: pylsp/hookspecs.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4-
# pylint: disable=redefined-builtin, unused-argument
54
from pylsp import hookspec
65

76

Diff for: pylsp/plugins/_resolvers.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4-
from collections import defaultdict
54
import logging
5+
from collections import defaultdict
66
from time import time
77

88
from jedi.api.classes import Completion
99

1010
from pylsp import lsp
1111

12-
1312
log = logging.getLogger(__name__)
1413

1514

@@ -77,7 +76,7 @@ def resolve(self, completion):
7776
try:
7877
sig = completion.get_signatures()
7978
return self.callback(completion, sig)
80-
except Exception as e: # pylint: disable=broad-except
79+
except Exception as e:
8180
log.warning(
8281
f"Something went wrong when resolving label for {completion}: {e}"
8382
)

Diff for: pylsp/plugins/_rope_task_handle.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from __future__ import annotations
22

33
import logging
4-
54
from typing import Callable, ContextManager, List, Optional, Sequence
65

76
from rope.base.taskhandle import BaseJobSet, BaseTaskHandle
87

9-
from pylsp.workspace import Workspace
108
from pylsp._utils import throttle
9+
from pylsp.workspace import Workspace
1110

1211
log = logging.getLogger(__name__)
1312
Report = Callable[[str, int], None]

Diff for: pylsp/plugins/autopep8_format.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import logging
55

66
import pycodestyle
7-
from autopep8 import fix_code, continued_indentation as autopep8_c_i
7+
from autopep8 import continued_indentation as autopep8_c_i
8+
from autopep8 import fix_code
89

910
from pylsp import hookimpl
1011
from pylsp._utils import get_eol_chars
@@ -13,18 +14,14 @@
1314

1415

1516
@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
16-
def pylsp_format_document(
17-
config, workspace, document, options
18-
): # pylint: disable=unused-argument
17+
def pylsp_format_document(config, workspace, document, options):
1918
with workspace.report_progress("format: autopep8"):
2019
log.info("Formatting document %s with autopep8", document)
2120
return _format(config, document)
2221

2322

2423
@hookimpl(tryfirst=True) # Prefer autopep8 over YAPF
25-
def pylsp_format_range(
26-
config, workspace, document, range, options
27-
): # pylint: disable=redefined-builtin,unused-argument
24+
def pylsp_format_range(config, workspace, document, range, options):
2825
log.info("Formatting document %s in range %s with autopep8", document, range)
2926

3027
# First we 'round' the range up/down to full lines only

Diff for: pylsp/plugins/definition.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33
from __future__ import annotations
4+
45
import logging
5-
from typing import Any, Dict, List, TYPE_CHECKING
6+
from typing import TYPE_CHECKING, Any, Dict, List
67

78
import jedi
89

9-
from pylsp import hookimpl, uris, _utils
10+
from pylsp import _utils, hookimpl, uris
1011

1112
if TYPE_CHECKING:
1213
from jedi.api import Script
1314
from jedi.api.classes import Name
15+
1416
from pylsp.config.config import Config
1517
from pylsp.workspace import Document
1618

Diff for: pylsp/plugins/flake8_lint.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ def run_flake8(flake8_executable, args, document, source):
141141
)
142142
cmd = [sys.executable, "-m", "flake8"]
143143
cmd.extend(args)
144-
p = Popen( # pylint: disable=consider-using-with
145-
cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, **popen_kwargs
146-
)
144+
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, **popen_kwargs)
147145
(stdout, stderr) = p.communicate(source.encode())
148146
if stderr:
149147
log.error("Error while running flake8 '%s'", stderr.decode())

Diff for: pylsp/plugins/highlight.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import logging
5-
from pylsp import hookimpl, lsp, _utils
5+
6+
from pylsp import _utils, hookimpl, lsp
67

78
log = logging.getLogger(__name__)
89

Diff for: pylsp/plugins/hover.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
from pylsp import hookimpl, _utils
6+
from pylsp import _utils, hookimpl
77

88
log = logging.getLogger(__name__)
99

Diff for: pylsp/plugins/jedi_completion.py

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
@hookimpl
3939
def pylsp_completions(config, document, position):
4040
"""Get formatted completions for current code position"""
41-
# pylint: disable=too-many-locals
4241
settings = config.plugin_settings("jedi_completion", document_path=document.path)
4342
resolve_eagerly = settings.get("eager", False)
4443
code_position = _utils.position_to_jedi_linecolumn(document, position)
@@ -209,7 +208,6 @@ def use_snippets(document, position):
209208

210209

211210
def _resolve_completion(completion, d, markup_kind: str):
212-
# pylint: disable=broad-except
213211
completion["detail"] = _detail(d)
214212
try:
215213
docs = _utils.format_docstring(

Diff for: pylsp/plugins/jedi_rename.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33

44
import logging
55

6-
from pylsp import hookimpl, uris, _utils
6+
from pylsp import _utils, hookimpl, uris
77

88
log = logging.getLogger(__name__)
99

1010

1111
@hookimpl
12-
def pylsp_rename(
13-
config, workspace, document, position, new_name
14-
): # pylint: disable=unused-argument
12+
def pylsp_rename(config, workspace, document, position, new_name):
1513
log.debug(
1614
"Executing rename of %s to %s", document.word_at_position(position), new_name
1715
)
@@ -20,7 +18,6 @@ def pylsp_rename(
2018
try:
2119
refactoring = document.jedi_script().rename(**kwargs)
2220
except NotImplementedError as exc:
23-
# pylint: disable=broad-exception-raised
2421
raise Exception(
2522
"No support for renaming in Python 2/3.5 with Jedi. "
2623
"Consider using the rope_rename plugin instead"

Diff for: pylsp/plugins/mccabe_lint.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import ast
55
import logging
6+
67
import mccabe
8+
79
from pylsp import hookimpl, lsp
810

911
log = logging.getLogger(__name__)

Diff for: pylsp/plugins/preload_imports.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright 2021- Python Language Server Contributors.
33

44
import logging
5+
56
from pylsp import hookimpl
67

78
log = logging.getLogger(__name__)
@@ -71,7 +72,7 @@ def pylsp_initialize(config):
7172
try:
7273
__import__(mod_name)
7374
log.debug("Preloaded module %s", mod_name)
74-
except Exception: # pylint: disable=broad-except
75+
except Exception:
7576
# Catch any exception since not only ImportError can be raised here
7677
# For example, old versions of NumPy can cause a ValueError.
7778
# See spyder-ide/spyder#13985

Diff for: pylsp/plugins/pydocstyle_lint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99

1010
import pydocstyle
11+
1112
from pylsp import hookimpl, lsp
1213

1314
log = logging.getLogger(__name__)
@@ -28,7 +29,6 @@ def pylsp_settings():
2829

2930
@hookimpl
3031
def pylsp_lint(config, workspace, document):
31-
# pylint: disable=too-many-locals
3232
with workspace.report_progress("lint: pydocstyle"):
3333
settings = config.plugin_settings("pydocstyle", document_path=document.path)
3434
log.debug("Got pydocstyle settings: %s", settings)

Diff for: pylsp/plugins/pyflakes_lint.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2017-2020 Palantir Technologies, Inc.
22
# Copyright 2021- Python Language Server Contributors.
33

4-
from pyflakes import api as pyflakes_api, messages
4+
from pyflakes import api as pyflakes_api
5+
from pyflakes import messages
6+
57
from pylsp import hookimpl, lsp
68

79
# Pyflakes messages that should be reported as Errors instead of Warns

0 commit comments

Comments
 (0)