Skip to content
This repository was archived by the owner on Feb 14, 2024. It is now read-only.

Commit cbcb474

Browse files
jtpiomartinRenou
andauthored
Adopt ruff, black, fix various issues (#169)
* Remove unused imports * More cleanups * Add black and ruff * Run black * Update ruff config * Fix some ruff * More fixes * More fixes * Fix more ruff * Run linter on CI * Run linting --------- Co-authored-by: martinRenou <martin.renou@gmail.com>
1 parent 96aea3e commit cbcb474

File tree

12 files changed

+125
-84
lines changed

12 files changed

+125
-84
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
set -eux
3030
jlpm
3131
jlpm run build
32-
jlpm run eslint:check
32+
jlpm run lint:check
3333
python -m pip install -v .
3434
jupyter labextension list 2>&1 | grep -ie "@jupyterlite/xeus-python-kernel.*OK"
3535

docs/conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
extensions = [
42
"jupyterlite_sphinx",
53
"myst_parser",

environment.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ dependencies:
1414
- empack >=3,<4
1515
- nodejs=20
1616
- mamba
17+
- black
18+
- ruff

jupyterlite_xeus_python/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# in editable mode with pip. It is highly recommended to install
66
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
77
import warnings
8-
warnings.warn("Importing 'jupyterlite-xeus-python' outside a proper installation.")
8+
9+
warnings.warn(
10+
"Importing 'jupyterlite-xeus-python' outside a proper installation.", stacklevel=2
11+
)
912
__version__ = "dev"
1013

1114

1215
def _jupyter_labextension_paths():
13-
return [{
14-
"src": "labextension",
15-
"dest": "@jupyterlite/xeus-python-kernel"
16-
}]
16+
return [{"src": "labextension", "dest": "@jupyterlite/xeus-python-kernel"}]

jupyterlite_xeus_python/build.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import csv
22
import os
3-
from copy import copy
4-
from pathlib import Path
5-
import requests
63
import shutil
7-
from subprocess import check_call, run, DEVNULL
4+
import sys
5+
from pathlib import Path
6+
from subprocess import run
87
from tempfile import TemporaryDirectory
9-
from typing import List
8+
from typing import List, Optional
109
from urllib.parse import urlparse
11-
import sys
1210

11+
import requests
12+
import typer
1313
import yaml
14-
15-
from empack.pack import pack_env, DEFAULT_CONFIG_PATH
1614
from empack.file_patterns import PkgFileFilter, pkg_file_filter_from_yaml
17-
18-
import typer
15+
from empack.pack import DEFAULT_CONFIG_PATH, pack_env
1916

2017
try:
2118
from mamba.api import create as mamba_create
@@ -40,6 +37,7 @@
4037
]
4138

4239
PLATFORM = "emscripten-32"
40+
DEFAULT_REQUEST_TIMEOUT = 1 # in minutes
4341

4442

4543
def create_env(
@@ -131,10 +129,12 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
131129
if log is not None:
132130
log.warning(
133131
"""
134-
Installing pip dependencies. This is very much experimental so use this feature at your own risks.
132+
Installing pip dependencies. This is very much experimental so use
133+
this feature at your own risks.
135134
Note that you can only install pure-python packages.
136-
pip is being run with the --no-deps option to not pull undesired system-specific dependencies, so please
137-
install your package dependencies from emscripten-forge or conda-forge.
135+
pip is being run with the --no-deps option to not pull undesired
136+
system-specific dependencies, so please install your package dependencies
137+
from emscripten-forge or conda-forge.
138138
"""
139139
)
140140

@@ -144,6 +144,8 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
144144

145145
run(
146146
[
147+
sys.executable,
148+
"-m",
147149
"pip",
148150
"install",
149151
*dependencies,
@@ -166,7 +168,7 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
166168
packages_dist_info = Path(pkg_dir.name).glob("*.dist-info")
167169

168170
for package_dist_info in packages_dist_info:
169-
with open(package_dist_info / "RECORD", "r") as record:
171+
with open(package_dist_info / "RECORD") as record:
170172
record_content = record.read()
171173
record_csv = csv.reader(record_content.splitlines())
172174
all_files = [_file[0] for _file in record_csv]
@@ -181,7 +183,7 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
181183
with open(package_dist_info / "RECORD", "w") as record:
182184
record.write(fixed_record_data)
183185

184-
non_supported_files = [".so", ".a", ".dylib", ".lib", ".exe" ".dll"]
186+
non_supported_files = [".so", ".a", ".dylib", ".lib", ".exe.dll"]
185187

186188
# COPY files under `prefix_path`
187189
for _file, inside_site_packages in files:
@@ -208,10 +210,10 @@ def _install_pip_dependencies(prefix_path, dependencies, log=None):
208210
shutil.copy(src_path, dest_path)
209211

210212

211-
def build_and_pack_emscripten_env(
213+
def build_and_pack_emscripten_env( # noqa: C901, PLR0912, PLR0915
212214
python_version: str = PYTHON_VERSION,
213215
xeus_python_version: str = XEUS_PYTHON_VERSION,
214-
packages: List[str] = [],
216+
packages: Optional[List[str]] = None,
215217
environment_file: str = "",
216218
root_prefix: str = "/tmp/xeus-python-kernel",
217219
env_name: str = "xeus-python-kernel",
@@ -222,13 +224,13 @@ def build_and_pack_emscripten_env(
222224
log=None,
223225
):
224226
"""Build a conda environment for the emscripten platform and pack it with empack."""
227+
if packages is None:
228+
packages = []
225229
channels = CHANNELS
226230
specs = [
227231
f"python={python_version}",
228232
"xeus-lite",
229-
"xeus-python"
230-
if not xeus_python_version
231-
else f"xeus-python={xeus_python_version}",
233+
"xeus-python" if not xeus_python_version else f"xeus-python={xeus_python_version}",
232234
*packages,
233235
]
234236
bail_early = True
@@ -261,13 +263,17 @@ def build_and_pack_emscripten_env(
261263
elif isinstance(dependency, dict) and dependency.get("pip") is not None:
262264
# If it's a local Python package, make its path relative to the environment file
263265
pip_dependencies = [
264-
((env_file.parent / pip_dep).resolve() if os.path.isdir(env_file.parent / pip_dep) else pip_dep)
266+
(
267+
(env_file.parent / pip_dep).resolve()
268+
if os.path.isdir(env_file.parent / pip_dep)
269+
else pip_dep
270+
)
265271
for pip_dep in dependency["pip"]
266272
]
267273

268274
# Bail early if there is nothing to do
269275
if bail_early and not force:
270-
return []
276+
return ""
271277

272278
orig_config = os.environ.get("CONDARC")
273279

@@ -294,7 +300,9 @@ def build_and_pack_emscripten_env(
294300
if empack_config:
295301
empack_config_is_url = urlparse(empack_config).scheme in ("http", "https")
296302
if empack_config_is_url:
297-
empack_config_content = requests.get(empack_config).content
303+
empack_config_content = requests.get(
304+
empack_config, timeout=DEFAULT_REQUEST_TIMEOUT
305+
).content
298306
pack_kwargs["file_filters"] = PkgFileFilter.parse_obj(
299307
yaml.safe_load(empack_config_content)
300308
)
@@ -324,7 +332,7 @@ def build_and_pack_emscripten_env(
324332
dirs_exist_ok=True,
325333
)
326334

327-
with open(Path(output_path) / "worker.ts", "r") as fobj:
335+
with open(Path(output_path) / "worker.ts") as fobj:
328336
worker = fobj.read()
329337

330338
worker = worker.replace("XEUS_KERNEL_FILE", "'xpython_wasm.js'")
@@ -356,9 +364,7 @@ def build_and_pack_emscripten_env(
356364
def main(
357365
python_version: str = PYTHON_VERSION,
358366
xeus_python_version: str = XEUS_PYTHON_VERSION,
359-
packages: List[str] = typer.Option(
360-
[], help="The list of packages you want to install"
361-
),
367+
packages: List[str] = typer.Option([], help="The list of packages you want to install"),
362368
environment_file: str = typer.Option(
363369
"", help="The path to the environment.yml file you want to use"
364370
),

jupyterlite_xeus_python/env_build_addon.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,17 @@
22
import json
33
import os
44
from pathlib import Path
5-
import requests
6-
import shutil
7-
from subprocess import check_call, run, DEVNULL
85
from tempfile import TemporaryDirectory
9-
from urllib.parse import urlparse
10-
11-
import yaml
12-
13-
from traitlets import List, Unicode
14-
15-
from empack.pack import pack_env, DEFAULT_CONFIG_PATH
16-
from empack.file_patterns import PkgFileFilter, pkg_file_filter_from_yaml
176

7+
from jupyterlite_core.addons.federated_extensions import FederatedExtensionAddon
188
from jupyterlite_core.constants import (
19-
SHARE_LABEXTENSIONS,
20-
LAB_EXTENSIONS,
9+
FEDERATED_EXTENSIONS,
2110
JUPYTERLITE_JSON,
11+
LAB_EXTENSIONS,
12+
SHARE_LABEXTENSIONS,
2213
UTF8,
23-
FEDERATED_EXTENSIONS,
2414
)
25-
from jupyterlite_core.addons.federated_extensions import FederatedExtensionAddon
15+
from traitlets import List, Unicode
2616

2717
from .build import XEUS_PYTHON_VERSION, build_and_pack_emscripten_env
2818

@@ -42,9 +32,8 @@ class XeusPythonEnv(FederatedExtensionAddon):
4232
)
4333

4434
empack_config = Unicode(
45-
None,
35+
"",
4636
config=True,
47-
allow_none=True,
4837
description="The path or URL to the empack config file",
4938
)
5039

@@ -64,7 +53,7 @@ class XeusPythonEnv(FederatedExtensionAddon):
6453
)
6554

6655
def __init__(self, *args, **kwargs):
67-
super(XeusPythonEnv, self).__init__(*args, **kwargs)
56+
super().__init__(*args, **kwargs)
6857

6958
self.cwd = TemporaryDirectory()
7059

@@ -133,9 +122,7 @@ def safe_copy_extension(self, pkg_json):
133122
stem = json.loads(pkg_json.read_text(**UTF8))["name"]
134123
dest = self.output_extensions / stem
135124
file_dep = [
136-
p
137-
for p in pkg_path.rglob("*")
138-
if not (p.is_dir() or self.is_ignored_sourcemap(p.name))
125+
p for p in pkg_path.rglob("*") if not (p.is_dir() or self.is_ignored_sourcemap(p.name))
139126
]
140127

141128
yield dict(

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@
4444
"eslint": "jlpm eslint:check --fix",
4545
"eslint:check": "eslint . --cache --ext .ts,.tsx",
4646
"install:extension": "jlpm build",
47-
"lint": "jlpm stylelint && jlpm prettier && jlpm eslint",
48-
"lint:check": "jlpm stylelint:check && jlpm prettier:check && jlpm eslint:check",
47+
"lint": "jlpm stylelint && jlpm prettier && jlpm eslint && jlpm lint:py",
48+
"lint:check": "jlpm stylelint:check && jlpm prettier:check && jlpm eslint:check && jlpm lint:py:check",
49+
"lint:py": "jlpm lint:py:black && jlpm lint:py:ruff --fix-only",
50+
"lint:py:check": "jlpm lint:py:black:check && jlpm lint:py:ruff:check",
51+
"lint:py:black": "black .",
52+
"lint:py:black:check": "black . --check",
53+
"lint:py:ruff": "ruff .",
54+
"lint:py:ruff:check": "ruff check .",
4955
"prettier": "jlpm prettier:base --write --list-different",
5056
"prettier:base": "prettier \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\"",
5157
"prettier:check": "jlpm prettier:base --check",

pyproject.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ dependencies = [
3030
]
3131
dynamic = ["version", "description", "authors", "urls", "keywords"]
3232

33+
[project.optional-dependencies]
34+
dev = [
35+
"black",
36+
"ruff==0.0.292",
37+
]
38+
3339
[project.scripts]
3440
jupyterlite-xeus-python-build = "jupyterlite_xeus_python.build:start"
3541

@@ -85,3 +91,51 @@ before-build-python = ["jlpm clean:all"]
8591

8692
[tool.check-wheel-contents]
8793
ignore = ["W002"]
94+
95+
[tool.black]
96+
line-length = 100
97+
target-version = ["py38"]
98+
99+
[tool.ruff]
100+
target-version = "py38"
101+
exclude = [
102+
"*/tests/*",
103+
"docs",
104+
]
105+
line-length = 100
106+
select = [
107+
"A", "B", "C", "DTZ", "E", "EM", "F", "FBT", "I", "ICN", "ISC", "N",
108+
"PLC", "PLE", "PLR", "PLW", "Q", "RUF", "S", "SIM", "T", "TID", "UP",
109+
"W", "YTT",
110+
]
111+
ignore = [
112+
"D100",
113+
"D104",
114+
# Q000 Single quotes found but double quotes preferred
115+
"Q000",
116+
# FBT001 Boolean positional arg in function definition
117+
"FBT001", "FBT002", "FBT003",
118+
# C408 Unnecessary `dict` call (rewrite as a literal)
119+
"C408", "C416",
120+
# allow for using print()
121+
"T201",
122+
# PLR0913 Too many arguments to function call
123+
"PLR0913",
124+
# extended flake8 ignore
125+
"D104", "D100", "EM101",
126+
# Probable insecure usage of temporary file or directory
127+
"S108",
128+
# RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
129+
"RUF012",
130+
]
131+
132+
[tool.ruff.per-file-ignores]
133+
# S101 Use of `assert` detected
134+
# F841 Local variable `foo` is assigned to but never used
135+
# PLR2004 Magic value used in comparison
136+
"tests/*" = ["S101", "F841", "PLR2004"]
137+
138+
# B008 Do not perform function call `typer.Option` in argument defaults
139+
# E501 `subprocess` call: check for execution of untrusted input
140+
# S603 `subprocess` call: check for execution of untrusted input
141+
"jupyterlite_xeus_python/build.py" = ["B008", "E501", "S603"]

style/base.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/*-----------------------------------------------------------------------------
1+
/* -----------------------------------------------------------------------------
22
| Copyright (c) Thorsten Beier
33
| Copyright (c) Jupyter Development Team.
44
| Distributed under the terms of the Modified BSD License.
5-
|----------------------------------------------------------------------------*/
5+
|---------------------------------------------------------------------------- */

style/index.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*-----------------------------------------------------------------------------
1+
/* -----------------------------------------------------------------------------
22
| Copyright (c) Thorsten Beier
33
| Copyright (c) Jupyter Development Team.
44
| Distributed under the terms of the Modified BSD License.
5-
|----------------------------------------------------------------------------*/
5+
|---------------------------------------------------------------------------- */
66

77
@import url('base.css');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
print('Hey')
1+
print("Hey")

0 commit comments

Comments
 (0)