Skip to content

Commit d2ec39e

Browse files
committed
Fix types and style
1 parent 22df561 commit d2ec39e

15 files changed

Lines changed: 64 additions & 96 deletions

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
fail-fast: false
2121
matrix:
2222
os: [ubuntu-latest, macos-latest, windows-latest]
23-
python-version: [ '3.9', '3.10.0', '3.11', '3.12', '3.13' ]
23+
python-version: [ '3.9', '3.10', '3.11', '3.12', '3.13' ]
2424

2525
steps:
2626
- name: Checkout
@@ -37,8 +37,8 @@ jobs:
3737
3838
- name: Check style and types
3939
run: |
40-
black --check src
41-
ruff check src
40+
ruff check --show-fixes
41+
ruff format --check
4242
mypy src
4343
4444
- name: Run tests

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.11.13
4+
hooks:
5+
- id: ruff-check
6+
args: [--fix, --show-fixes]
7+
- id: ruff-format

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ dependencies = [
3737
test = [
3838
"mypy",
3939
"ruff",
40-
"black",
4140
"pytest",
4241
"pytest-asyncio",
4342
"pytest-rerunfailures",
@@ -71,3 +70,4 @@ Homepage = "https://github.com/davidbrochart/akernel"
7170

7271
[tool.ruff]
7372
line-length = 100
73+
exclude = ["examples"]

src/akernel/cache.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
def cache(cache_dir: str | None):
1212
if not cache_dir:
13-
cache_dir = os.path.join(
14-
sys.prefix, "share", "jupyter", "kernels", "akernel", "cache"
15-
)
13+
cache_dir = os.path.join(sys.prefix, "share", "jupyter", "kernels", "akernel", "cache")
1614

1715
l4 = File(cache_dir)
1816
l3 = Func(zlib.compress, zlib.decompress, l4)

src/akernel/code.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ def get_return_body(val):
9292

9393

9494
class Transform:
95-
def __init__(
96-
self, code: str, task_i: int | None = None, react: bool = False
97-
) -> None:
95+
def __init__(self, code: str, task_i: int | None = None, react: bool = False) -> None:
9896
self.gtree = gast.parse(code)
9997
self.task_i = task_i
10098
self.react = react
@@ -120,9 +118,7 @@ def get_async_ast(self) -> gast.Module:
120118
new_body += self.gtree.body + body_globals_update_locals + last_statement
121119
else:
122120
new_body += self.gtree.body + body_globals_update_locals
123-
name = (
124-
"__async_cell__" if self.task_i is None else f"__async_cell{self.task_i}__"
125-
)
121+
name = "__async_cell__" if self.task_i is None else f"__async_cell{self.task_i}__"
126122
body = [
127123
gast.AsyncFunctionDef(
128124
name=name,
@@ -169,16 +165,12 @@ def make_react(self):
169165
):
170166
# RHS
171167
rhs_calls = [
172-
n
173-
for n in gast.walk(statement.value)
174-
if isinstance(n, gast.Call)
168+
n for n in gast.walk(statement.value) if isinstance(n, gast.Call)
175169
]
176170
for n in rhs_calls:
177171
ipyx_name = gast.Name(id="ipyx", ctx=gast.Load())
178172
n.func = gast.Call(
179-
func=gast.Attribute(
180-
value=ipyx_name, attr="F", ctx=gast.Load()
181-
),
173+
func=gast.Attribute(value=ipyx_name, attr="F", ctx=gast.Load()),
182174
args=[n.func],
183175
keywords=[],
184176
)
@@ -243,9 +235,7 @@ def visit_Name(self, node):
243235
def visit_Assign(self, node):
244236
ctx, g = self.context[-1]
245237
if ctx == "global":
246-
self.outputs += [
247-
target.id for target in node.targets if isinstance(target, gast.Name)
248-
]
238+
self.outputs += [target.id for target in node.targets if isinstance(target, gast.Name)]
249239
self.generic_visit(node)
250240

251241
def visit_AugAssign(self, node):

src/akernel/comm/comm.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

3-
from typing import Dict, List, Any, Callable
3+
from typing import Any, Callable
44

5-
import comm # type: ignore
5+
import comm
66

77
from ..message import send_message, create_message
88

@@ -11,9 +11,9 @@ class Comm(comm.base_comm.BaseComm):
1111
_msg_callback: Callable | None
1212
comm_id: str
1313
topic: bytes
14-
parent_header: Dict[str, Any]
14+
parent_header: dict[str, Any]
1515

16-
def __init__(self, **kwargs):
16+
def __init__(self, **kwargs) -> None:
1717
from akernel.kernel import KERNEL, PARENT_VAR, Kernel
1818

1919
self.kernel: Kernel = KERNEL
@@ -23,10 +23,10 @@ def __init__(self, **kwargs):
2323
def publish_msg(
2424
self,
2525
msg_type: str,
26-
data: Dict[str, Any],
27-
metadata: Dict[str, Any],
28-
buffers: List[bytes],
29-
**keys,
26+
data: dict[str, Any] | None = None,
27+
metadata: dict[str, Any] | None = None,
28+
buffers: list[bytes] | None = None,
29+
**keys: Any,
3030
) -> None:
3131
msg = create_message(
3232
msg_type,
@@ -42,7 +42,7 @@ def publish_msg(
4242
buffers=buffers,
4343
)
4444

45-
def handle_msg(self, msg: Dict[str, Any]) -> None:
45+
def handle_msg(self, msg: dict[str, Any]) -> None:
4646
if self._msg_callback:
4747
self.kernel.execution_state = "busy"
4848
msg2 = create_message(

src/akernel/comm/manager.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
from typing import Dict, Callable
1+
from typing import Dict, Callable, cast
22

3-
import comm # type: ignore
3+
import comm
44

55
from .comm import Comm
66

77

88
class CommManager(comm.CommManager):
9-
comms: Dict[str, Comm]
9+
comms: dict[str, comm.base_comm.BaseComm]
1010
targets: Dict[str, Callable]
1111

12-
def __init__(self):
12+
def __init__(self) -> None:
1313
super().__init__()
1414
from akernel.kernel import KERNEL, Kernel
1515

1616
self.kernel: Kernel = KERNEL
1717

18-
def register_comm(self, comm: Comm) -> str:
18+
def register_comm(self, comm: comm.base_comm.BaseComm) -> str:
19+
comm = cast(Comm, comm)
1920
comm_id = comm.comm_id
2021
comm.kernel = self.kernel
2122
self.comms[comm_id] = comm

src/akernel/execution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def pre_execute(
4343
f"{Style.RESET_ALL}:",
4444
f"{Fore.RED}{exception.text.rstrip()}{Style.RESET_ALL}",
4545
(exception.offset - 1) * " " + "^",
46-
f"{Fore.RED}{type(exception).__name__}{Style.RESET_ALL}: "
47-
f"{exception.args[0]}",
46+
f"{Fore.RED}{type(exception).__name__}{Style.RESET_ALL}: {exception.args[0]}",
4847
]
4948
else:
5049
if cache is not None:

src/akernel/kernel.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ def get_comm_manager():
102102
self.iopub_channel = connect_channel("iopub", self.connection_cfg)
103103
self.control_channel = connect_channel("control", self.connection_cfg)
104104
self.stdin_channel = connect_channel("stdin", self.connection_cfg)
105-
msg = self.create_message(
106-
"status", content={"execution_state": self.execution_state}
107-
)
105+
msg = self.create_message("status", content={"execution_state": self.execution_state})
108106
send_message(msg, self.iopub_channel, self.key)
109107
self.execution_state = "idle"
110108
self.stop = asyncio.Event()
@@ -166,8 +164,7 @@ def init_kernel(self, namespace):
166164
self.locals[namespace] = {}
167165
if self.react_kernel:
168166
code = (
169-
"import ipyx, ipywidgets;"
170-
"globals().update({'ipyx': ipyx, 'ipywidgets': ipywidgets})"
167+
"import ipyx, ipywidgets;globals().update({'ipyx': ipyx, 'ipywidgets': ipywidgets})"
171168
)
172169
exec(code, self.globals[namespace], self.locals[namespace])
173170

@@ -314,10 +311,7 @@ async def listen_shell(self) -> None:
314311
parent_header=parent_header,
315312
content={
316313
"status": "ok",
317-
"comms": {
318-
comm_id: {"target_name": target_name}
319-
for comm_id in comms
320-
},
314+
"comms": {comm_id: {"target_name": target_name} for comm_id in comms},
321315
},
322316
)
323317
send_message(msg2, self.shell_channel, self.key, idents[0])
@@ -329,7 +323,7 @@ async def listen_shell(self) -> None:
329323
)
330324
send_message(msg2, self.iopub_channel, self.key)
331325
elif msg_type == "comm_msg":
332-
self.comm_manager.comm_msg(None, None, msg)
326+
self.comm_manager.comm_msg(None, None, msg) # type: ignore[arg-type]
333327

334328
async def listen_control(self) -> None:
335329
while True:

src/akernel/kernelspec.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import json
66

77

8-
def write_kernelspec(
9-
dir_name: str, mode: str, display_name: str, cache_dir: str | None
10-
) -> None:
8+
def write_kernelspec(dir_name: str, mode: str, display_name: str, cache_dir: str | None) -> None:
119
argv = ["akernel", "launch"]
1210
if mode:
1311
argv.append(mode)

0 commit comments

Comments
 (0)