Skip to content

Commit 0a08ba3

Browse files
authored
Update ruff config (#7190)
* Update ruff config * cleanup
1 parent 5ec7d5e commit 0a08ba3

File tree

8 files changed

+64
-58
lines changed

8 files changed

+64
-58
lines changed

.pre-commit-config.yaml

+2-8
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ repos:
2121
- id: trailing-whitespace
2222

2323
- repo: https://github.com/python-jsonschema/check-jsonschema
24-
rev: 0.27.2
24+
rev: 0.27.3
2525
hooks:
2626
- id: check-github-workflows
2727

28-
- repo: https://github.com/adamchainz/blacken-docs
29-
rev: '1.16.0'
30-
hooks:
31-
- id: blacken-docs
32-
additional_dependencies: [black==23.7.0]
33-
3428
- repo: https://github.com/codespell-project/codespell
3529
rev: 'v2.2.6'
3630
hooks:
@@ -60,7 +54,7 @@ repos:
6054
- id: rst-inline-touching-normal
6155

6256
- repo: https://github.com/astral-sh/ruff-pre-commit
63-
rev: v0.1.6
57+
rev: v0.1.8
6458
hooks:
6559
- id: ruff
6660
types_or: [ python, jupyter ]

docs/source/conf.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python3
2-
#
31
# Jupyter Notebook documentation build configuration file, created by
42
# sphinx-quickstart on Mon Apr 13 09:51:11 2015.
53
#
@@ -73,7 +71,7 @@
7371
]
7472

7573
try:
76-
import enchant # type:ignore # noqa
74+
import enchant # noqa: F401
7775

7876
extensions += ["sphinxcontrib.spelling"]
7977
except ImportError:
@@ -97,7 +95,7 @@
9795

9896
# General information about the project.
9997
project = "Jupyter Notebook"
100-
copyright = "2015, Jupyter Team, https://jupyter.org" # noqa
98+
copyright = "2015, Jupyter Team, https://jupyter.org"
10199
author = "The Jupyter Team"
102100

103101
# ghissue config
@@ -109,7 +107,7 @@
109107
#
110108
_version_py = os.path.join(here, "../../notebook/_version.py")
111109
version_ns = {}
112-
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns) # noqa
110+
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns) # noqa: S102, SIM115
113111
# The short X.Y version.
114112
version = "%i.%i" % version_ns["version_info"][:2]
115113
# The full version, including alpha/beta/rc tags.

notebook/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Any
44

5-
from ._version import __version__ # noqa
5+
from ._version import __version__ # noqa: F401
66

77

88
def _jupyter_server_extension_paths() -> list[dict[str, str]]:

notebook/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
_version_fields = _version_regex.match(__version__).groupdict() # type:ignore[union-attr]
2525

26-
VersionInfo = namedtuple("VersionInfo", ["major", "minor", "micro", "releaselevel", "serial"])
26+
VersionInfo = namedtuple("VersionInfo", ["major", "minor", "micro", "releaselevel", "serial"]) # noqa: PYI024
2727

2828
version_info = VersionInfo(
2929
*[

notebook/app.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import re
66
import typing as t
7-
from os.path import join as pjoin
7+
from pathlib import Path
88

99
from jupyter_client.utils import ensure_async # type:ignore[attr-defined]
1010
from jupyter_core.application import base_aliases
@@ -36,11 +36,11 @@
3636

3737
from ._version import __version__
3838

39-
HERE = os.path.dirname(__file__)
39+
HERE = Path(__file__).parent.resolve()
4040

4141
Flags = t.Dict[t.Union[str, t.Tuple[str, ...]], t.Tuple[t.Union[t.Dict[str, t.Any], Config], str]]
4242

43-
app_dir = get_app_dir()
43+
app_dir = Path(get_app_dir())
4444
version = __version__
4545

4646
# mypy: disable-error-code="no-untyped-call"
@@ -72,7 +72,7 @@ def get_page_config(self) -> dict[str, t.Any]:
7272

7373
server_root = self.settings.get("server_root_dir", "")
7474
server_root = server_root.replace(os.sep, "/")
75-
server_root = os.path.normpath(os.path.expanduser(server_root))
75+
server_root = os.path.normpath(Path(server_root).expanduser())
7676
try:
7777
# Remove the server_root from pref dir
7878
if self.serverapp.preferred_dir != server_root:
@@ -149,7 +149,7 @@ async def get(self, path: str = "") -> None:
149149

150150
tpl = self.render_template("tree.html", page_config=page_config)
151151
return self.write(tpl)
152-
elif await ensure_async(cm.file_exists(path)):
152+
if await ensure_async(cm.file_exists(path)):
153153
# it's not a directory, we have redirecting to do
154154
model = await ensure_async(cm.get(path, content=False))
155155
if model["type"] == "notebook":
@@ -159,15 +159,15 @@ async def get(self, path: str = "") -> None:
159159
url = ujoin(self.base_url, "files", url_escape(path))
160160
self.log.debug("Redirecting %s to %s", self.request.path, url)
161161
self.redirect(url)
162-
else:
163-
raise web.HTTPError(404)
162+
return None
163+
raise web.HTTPError(404)
164164

165165

166166
class ConsoleHandler(NotebookBaseHandler):
167167
"""A console page handler."""
168168

169169
@web.authenticated
170-
def get(self, path: str | None = None) -> t.Any:
170+
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002
171171
"""Get the console page."""
172172
tpl = self.render_template("consoles.html", page_config=self.get_page_config())
173173
return self.write(tpl)
@@ -177,7 +177,7 @@ class TerminalHandler(NotebookBaseHandler):
177177
"""A terminal page handler."""
178178

179179
@web.authenticated
180-
def get(self, path: str | None = None) -> t.Any:
180+
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002
181181
"""Get the terminal page."""
182182
tpl = self.render_template("terminals.html", page_config=self.get_page_config())
183183
return self.write(tpl)
@@ -187,7 +187,7 @@ class FileHandler(NotebookBaseHandler):
187187
"""A file page handler."""
188188

189189
@web.authenticated
190-
def get(self, path: str | None = None) -> t.Any:
190+
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002
191191
"""Get the file page."""
192192
tpl = self.render_template("edit.html", page_config=self.get_page_config())
193193
return self.write(tpl)
@@ -197,7 +197,7 @@ class NotebookHandler(NotebookBaseHandler):
197197
"""A notebook page handler."""
198198

199199
@web.authenticated
200-
def get(self, path: str | None = None) -> t.Any:
200+
def get(self, path: str | None = None) -> t.Any: # noqa: ARG002
201201
"""Get the notebook page."""
202202
tpl = self.render_template("notebooks.html", page_config=self.get_page_config())
203203
return self.write(tpl)
@@ -214,13 +214,13 @@ def get(self) -> t.Any:
214214
page_config = self.get_page_config()
215215
custom_css_file = f"{page_config['jupyterConfigDir']}/custom/custom.css"
216216

217-
if not os.path.isfile(custom_css_file):
217+
if not Path(custom_css_file).is_file():
218218
static_path_root = re.match("^(.*?)static", page_config["staticDir"])
219219
if static_path_root is not None:
220220
custom_dir = static_path_root.groups()[0]
221221
custom_css_file = f"{custom_dir}custom/custom.css"
222222

223-
with open(custom_css_file) as css_f:
223+
with Path(custom_css_file).open() as css_f:
224224
return self.write(css_f.read())
225225

226226

@@ -269,23 +269,23 @@ class JupyterNotebookApp(NotebookConfigShimMixin, LabServerApp): # type:ignore[
269269

270270
@default("static_dir")
271271
def _default_static_dir(self) -> str:
272-
return os.path.join(HERE, "static")
272+
return str(HERE / "static")
273273

274274
@default("templates_dir")
275275
def _default_templates_dir(self) -> str:
276-
return os.path.join(HERE, "templates")
276+
return str(HERE / "templates")
277277

278278
@default("app_settings_dir")
279279
def _default_app_settings_dir(self) -> str:
280-
return pjoin(app_dir, "settings")
280+
return str(app_dir / "settings")
281281

282282
@default("schemas_dir")
283283
def _default_schemas_dir(self) -> str:
284-
return pjoin(app_dir, "schemas")
284+
return str(app_dir / "schemas")
285285

286286
@default("themes_dir")
287287
def _default_themes_dir(self) -> str:
288-
return pjoin(app_dir, "themes")
288+
return str(app_dir / "themes")
289289

290290
@default("user_settings_dir")
291291
def _default_user_settings_dir(self) -> str:
@@ -343,7 +343,7 @@ def initialize_handlers(self) -> None:
343343
self.handlers.append(("/custom/custom.css", CustomCssHandler))
344344
super().initialize_handlers()
345345

346-
def initialize(self, argv: list[str] | None = None) -> None:
346+
def initialize(self, argv: list[str] | None = None) -> None: # noqa: ARG002
347347
"""Subclass because the ExtensionApp.initialize() method does not take arguments"""
348348
super().initialize()
349349

pyproject.toml

+32-18
Original file line numberDiff line numberDiff line change
@@ -231,38 +231,52 @@ source = ["notebook"]
231231
files = "notebook"
232232
python_version = "3.8"
233233
strict = true
234-
hide_error_codes = false
235234
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
236235
warn_unreachable = true
237236

238237
[tool.ruff]
239238
line-length = 100
240239

240+
[tool.ruff.format]
241+
docstring-code-format = true
242+
241243
[tool.ruff.lint]
242-
select = [
243-
"A", "B", "C", "DTZ", "E", "EM", "F", "FBT", "I", "ICN", "N",
244-
"PLC", "PLE", "PLR", "PLW", "Q", "RUF", "S", "SIM", "T", "TID", "UP",
245-
"W", "YTT",
244+
extend-select = [
245+
"B", # flake8-bugbear
246+
"I", # isort
247+
"ARG", # flake8-unused-arguments
248+
"C4", # flake8-comprehensions
249+
"EM", # flake8-errmsg
250+
"ICN", # flake8-import-conventions
251+
"G", # flake8-logging-format
252+
"PGH", # pygrep-hooks
253+
"PIE", # flake8-pie
254+
"PL", # pylint
255+
"PTH", # flake8-use-pathlib
256+
"PT", # flake8-pytest-style
257+
"RET", # flake8-return
258+
"RUF", # Ruff-specific
259+
"SIM", # flake8-simplify
260+
"T20", # flake8-print
261+
"UP", # pyupgrade
262+
"YTT", # flake8-2020
263+
"EXE", # flake8-executable
264+
"PYI", # flake8-pyi
265+
"S", # flake8-bandit
246266
]
247267
ignore = [
248-
# Q000 Single quotes found but double quotes preferred
249-
"Q000",
250-
# FBT001 Boolean positional arg in function definition
251-
"FBT001", "FBT002", "FBT003",
252-
# C408 Unnecessary `dict` call (rewrite as a literal)
253-
"C408", "C416",
254-
# RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
255-
"RUF012",
268+
"PLR", # Design related pylint codes
269+
"C408", "C416", # Unnecessary `dict` call (rewrite as a literal)
270+
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
256271
]
257272

258273
[tool.ruff.lint.per-file-ignores]
259274
# S101 Use of `assert` detected
260275
# F841 Local variable `foo` is assigned to but never used
261-
# PLR2004 Magic value used in comparison
262-
"tests/*" = ["S101", "F841", "PLR2004"]
263-
# undefined name 'c'
276+
"tests/*" = ["S101", "F841", "ARG", "PTH"]
277+
"docs/source/conf.py" = ["PTH"]
264278
"ui-tests/test/jupyter_server_config.py" = ["F821"]
265-
"*.ipynb" = ["E402", "B018", "E501", "T201"]
279+
"*.ipynb" = ["E402", "B018", "E501", "T201", "RET"]
266280

267281
[tool.interrogate]
268282
ignore-init-module=true
@@ -275,4 +289,4 @@ fail-under=100
275289
exclude = ["tests", "ui-tests", "docs", "node_modules", "setup.py"]
276290

277291
[tool.repo-review]
278-
ignore = ["PY007", "GH102", "PC180"]
292+
ignore = ["GH102", "PC180", "PC111"]

tests/conftest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def mkdir(tmp_path, *parts):
3232
labextensions_dir = pytest.fixture(lambda tmp_path: mkdir(tmp_path, "labextensions_dir"))
3333

3434

35-
@pytest.fixture
36-
def make_notebook_app( # noqa PLR0913
35+
@pytest.fixture()
36+
def make_notebook_app( # PLR0913
3737
jp_root_dir,
3838
jp_template_dir,
3939
app_settings_dir,
@@ -92,7 +92,7 @@ def _make_notebook_app(**kwargs):
9292
)
9393

9494
# Copy the schema files.
95-
test_data = str(files("jupyterlab_server.test_data")._paths[0]) # type: ignore
95+
test_data = str(files("jupyterlab_server.test_data")._paths[0])
9696
src = pathlib.PurePath(test_data, "schemas", "@jupyterlab")
9797
dst = pathlib.PurePath(str(schemas_dir), "@jupyterlab")
9898
if os.path.exists(dst):
@@ -131,7 +131,7 @@ def _make_notebook_app(**kwargs):
131131
return _make_notebook_app
132132

133133

134-
@pytest.fixture
134+
@pytest.fixture()
135135
def notebookapp(jp_serverapp, make_notebook_app):
136136
app = make_notebook_app()
137137
app._link_jupyter_server_extension(jp_serverapp)

tests/test_app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from notebook.app import JupyterNotebookApp, TreeHandler
77

88

9-
@pytest.fixture
9+
@pytest.fixture()
1010
def notebooks(jp_create_notebook, notebookapp):
1111
nbpaths = (
1212
"notebook1.ipynb",
@@ -48,7 +48,7 @@ def redirect(self, url):
4848
nonlocal redirected_url
4949
redirected_url = url
5050

51-
TreeHandler.redirect = redirect # type:ignore
51+
TreeHandler.redirect = redirect
5252
await jp_fetch("tree", "notebook1.ipynb")
5353
assert redirected_url == "/a%40b/notebooks/notebook1.ipynb"
5454

0 commit comments

Comments
 (0)