Skip to content
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

Update ruff config #101

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.1
rev: 0.27.3
hooks:
- id: check-github-workflows

Expand Down Expand Up @@ -56,7 +56,7 @@ repos:
- id: rst-inline-touching-normal

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.7.0"
rev: "v1.7.1"
hooks:
- id: mypy
files: "^jupyter_server_terminals"
Expand All @@ -66,7 +66,7 @@ repos:
["traitlets>=5.13", "jupyter_server>=2.10.1", "terminado>=0.18"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.5
rev: v0.1.7
hooks:
- id: ruff
types_or: [python, jupyter]
Expand All @@ -75,7 +75,7 @@ repos:
types_or: [python, jupyter]

- repo: https://github.com/scientific-python/cookie
rev: "2023.10.27"
rev: "2023.11.17"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
6 changes: 2 additions & 4 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
#
# Jupyter Server documentation build configuration file, created by
# sphinx-quickstart on Mon Apr 13 09:51:11 2015.
#
Expand Down Expand Up @@ -68,7 +66,7 @@
]

try:
import enchant # type:ignore # noqa
import enchant # type:ignore[import] # noqa: F401

extensions += ["sphinxcontrib.spelling"]
except ImportError:
Expand All @@ -92,7 +90,7 @@

# General information about the project.
project = "Jupyter Server Terminals"
copyright = "2021, Jupyter Team, https://jupyter.org" # noqa
copyright = "2021, Jupyter Team, https://jupyter.org"
author = "The Jupyter Server Team"

# ghissue config
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server_terminals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
msg = "Jupyter Server must be installed to use this extension."
raise ModuleNotFoundError(msg) from None

if int(version_info[0]) < 2: # type:ignore[call-overload] # noqa
if int(version_info[0]) < 2: # type:ignore[call-overload]
msg = "Jupyter Server Terminals requires Jupyter Server 2.0+"
raise RuntimeError(msg)

Expand Down
8 changes: 5 additions & 3 deletions jupyter_server_terminals/api_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ def post(self) -> None:
if cwd is None:
server_root_dir = self.settings["server_root_dir"]
self.log.debug(
f"Failed to find requested terminal cwd: {data.get('cwd')}\n"
f" It was not found within the server root neither: {server_root_dir}."
"Failed to find requested terminal cwd: %s\n"
" It was not found within the server root neither: %s.",
data.get("cwd"),
server_root_dir,
)
del data["cwd"]
else:
self.log.debug(f"Opening terminal in: {cwd.resolve()!s}")
self.log.debug("Opening terminal in: %s", cwd.resolve())
data["cwd"] = str(cwd.resolve())

model = self.terminal_manager.create(**data)
Expand Down
6 changes: 3 additions & 3 deletions jupyter_server_terminals/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TerminalsExtensionApp(ExtensionApp):

def initialize_settings(self) -> None:
"""Initialize settings."""
if not self.serverapp.terminals_enabled:
if not self.serverapp or not self.serverapp.terminals_enabled:
return
self.initialize_configurables()
self.settings.update(
Expand Down Expand Up @@ -73,7 +73,7 @@ def initialize_configurables(self) -> None:

def initialize_handlers(self) -> None:
"""Initialize handlers."""
if not self.serverapp.terminals_enabled:
if not self.serverapp or not self.serverapp.terminals_enabled:
# Checking self.terminals_available instead breaks enabling terminals
return
self.handlers.append(
Expand Down Expand Up @@ -112,7 +112,7 @@ async def cleanup_terminals(self) -> None:
terminal_msg = trans.ngettext(
"Shutting down %d terminal", "Shutting down %d terminals", n_terminals
)
self.log.info(terminal_msg % n_terminals)
self.log.info("%s %% %s", terminal_msg, n_terminals)
await ensure_async(terminal_manager.terminate_all()) # type:ignore[arg-type]

async def stop_extension(self) -> None:
Expand Down
14 changes: 7 additions & 7 deletions jupyter_server_terminals/terminalmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ def create(self, **kwargs: t.Any) -> MODEL:

def get(self, name: str) -> MODEL:
"""Get terminal 'name'."""
model = self.get_terminal_model(name)
return model
return self.get_terminal_model(name)

def list(self) -> list[MODEL]: # noqa
def list(self) -> list[MODEL]:
"""Get a list of all running terminals."""
models = [self.get_terminal_model(name) for name in self.terminals]

Expand Down Expand Up @@ -94,11 +93,10 @@ def get_terminal_model(self, name: str) -> MODEL:
"""
self._check_terminal(name)
term = self.terminals[name]
model = {
return {
"name": name,
"last_activity": isoformat(term.last_activity), # type:ignore[attr-defined]
}
return model

def _check_terminal(self, name: str) -> None:
"""Check a that terminal 'name' exists and raise 404 if not."""
Expand All @@ -109,7 +107,7 @@ def _initialize_culler(self) -> None:
"""Start culler if 'cull_inactive_timeout' is greater than zero.
Regardless of that value, set flag that we've been here.
"""
if not self._initialized_culler and self.cull_inactive_timeout > 0: # noqa
if not self._initialized_culler and self.cull_inactive_timeout > 0: # noqa: SIM102
if self._culler_callback is None:
_ = IOLoop.current()
if self.cull_interval <= 0: # handle case where user set invalid value
Expand Down Expand Up @@ -144,7 +142,9 @@ async def _cull_terminals(self) -> None:
except Exception as e:
self.log.exception(
"The following exception was encountered while checking the "
f"activity of terminal {name}: {e}"
"activity of terminal %s: %s",
name,
e,
)

async def _cull_inactive_terminal(self, name: str) -> None:
Expand Down
51 changes: 29 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ source = ["jupyter_server_terminals"]
files = "jupyter_server_terminals"
python_version = "3.8"
strict = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
warn_unreachable = true

Expand All @@ -134,26 +133,34 @@ warn_unreachable = true
line-length = 100

[tool.ruff.lint]
select = [
"A", "B", "C", "DTZ", "E", "EM", "F", "FBT", "I", "ICN", "N",
"PLC", "PLE", "PLR", "PLW", "Q", "RUF", "S", "SIM", "T", "TID", "UP",
"W", "YTT",
extend-select = [
"B", # flake8-bugbear
"I", # isort
"C4", # flake8-comprehensions
"EM", # flake8-errmsg
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # pylint
"PTH", # flake8-use-pathlib
"PT", # flake8-pytest-style
"RET", # flake8-return
"RUF", # Ruff-specific
"SIM", # flake8-simplify
"T20", # flake8-print
"UP", # pyupgrade
"YTT", # flake8-2020
"EXE", # flake8-executable
"PYI", # flake8-pyi
"S", # flake8-bandit
]
ignore = [
# Q000 Single quotes found but double quotes preferred
"Q000",
# FBT001 Boolean positional arg in function definition
"FBT001", "FBT002", "FBT003",
# E501 Line too long (158 > 100 characters)
"E501",
# SIM105 Use `contextlib.suppress(...)`
"SIM105",
# T201 `print` found
"T201",
# N802 Function name `CreateWellKnownSid` should be lowercase
"N802", "N803",
# S101 Use of `assert` detected
"S101",
"PLR", # Design related pylint codes
"E501", # Line too long (158 > 100 characters)
"SIM105", # Use `contextlib.suppress(...)`
"T201", # `print` found
"S101", # Use of `assert` detected
]
unfixable = [
# Don't touch print statements
Expand All @@ -166,8 +173,8 @@ unfixable = [
# B011: Do not call assert False since python -O removes these calls
# F841 local variable 'foo' is assigned to but never used
# S101 Use of `assert` detected
# PLR2004 Magic value used in comparison
"tests/*" = ["B011", "F841", "S101", "PLR2004"]
"tests/*" = ["B011", "F841"]
"docs/*" = ["PTH"]

[tool.interrogate]
ignore-init-module=true
Expand All @@ -180,4 +187,4 @@ fail-under=100
exclude = ["tests", "docs"]

[tool.repo-review]
ignore = ["PY007", "GH102"]
ignore = ["GH102"]
12 changes: 6 additions & 6 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AuthorizerforTesting(Authorizer):
# Set these class attributes from within a test
# to verify that they match the arguments passed
# by the REST API.
permissions: Dict[str, str] = {} # noqa
permissions: Dict[str, str] = {} # noqa: RUF012

def normalize_url(self, path):
"""Drop the base URL and make sure path leads with a /"""
Expand Down Expand Up @@ -50,7 +50,7 @@ def is_authorized(self, handler, user, action, resource):
)


@pytest.fixture
@pytest.fixture()
def jp_server_config():
return Config(
{
Expand All @@ -62,7 +62,7 @@ def jp_server_config():
)


@pytest.fixture
@pytest.fixture()
def send_request(jp_fetch, jp_ws_fetch):
"""Send to Jupyter Server and return response code."""

Expand Down Expand Up @@ -109,9 +109,9 @@ async def _(url, **fetch_kwargs):
# -------- Test scenarios -----------


@pytest.mark.parametrize("method, url, body", HTTP_REQUESTS_PARAMETRIZED)
@pytest.mark.parametrize("allowed", (True, False))
async def test_authorized_requests( # noqa
@pytest.mark.parametrize("method, url, body", HTTP_REQUESTS_PARAMETRIZED) # noqa: PT006
@pytest.mark.parametrize("allowed", (True, False)) # noqa: PT007
async def test_authorized_requests(
request,
io_loop,
send_request,
Expand Down
13 changes: 7 additions & 6 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import os
import shutil
import sys
from pathlib import Path

import pytest
from tornado.httpclient import HTTPClientError
from traitlets.config.loader import Config


@pytest.fixture
@pytest.fixture()
def terminal_path(tmp_path):
subdir = tmp_path.joinpath("terminal_path")
subdir.mkdir()
Expand All @@ -19,7 +20,7 @@ def terminal_path(tmp_path):
shutil.rmtree(str(subdir), ignore_errors=True)


@pytest.fixture
@pytest.fixture()
def terminal_root_dir(jp_root_dir):
subdir = jp_root_dir.joinpath("terminal_path")
subdir.mkdir()
Expand All @@ -33,7 +34,7 @@ def terminal_root_dir(jp_root_dir):
CULL_INTERVAL = 3


@pytest.fixture
@pytest.fixture()
def jp_server_config():
return Config(
{
Expand Down Expand Up @@ -148,7 +149,7 @@ async def test_terminal_create_with_cwd(jp_fetch, jp_ws_fetch, terminal_path):

ws.close()

assert os.path.basename(terminal_path) in message_stdout
assert Path(terminal_path).name in message_stdout


async def test_terminal_create_with_relative_cwd(
Expand Down Expand Up @@ -195,7 +196,7 @@ async def test_terminal_create_with_relative_cwd(


async def test_terminal_create_with_bad_cwd(jp_fetch, jp_ws_fetch):
non_existing_path = "/tmp/path/to/nowhere" # noqa
non_existing_path = "/tmp/path/to/nowhere" # noqa: S108
resp = await jp_fetch(
"api",
"terminals",
Expand Down Expand Up @@ -268,7 +269,7 @@ async def test_culling(jp_fetch):
allow_nonstandard_methods=True,
)
except HTTPClientError as e:
assert e.code == 404
assert e.code == 404 # noqa: PT017
culled = True
break
else:
Expand Down
Loading