Skip to content

Commit a32c3c1

Browse files
authored
Adopt ruff and handle lint (#1925)
* adopt ruff and handle lint * fix failing test
1 parent 91bfdfb commit a32c3c1

37 files changed

+506
-457
lines changed

.github/workflows/tests.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ jobs:
5757
pip install codecov coverage[toml]
5858
codecov
5959
60+
test_lint:
61+
name: Test Lint
62+
runs-on: ubuntu-latest
63+
steps:
64+
- uses: actions/checkout@v3
65+
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
66+
- name: Run Linters
67+
run: |
68+
hatch run lint:style
69+
pipx run 'validate-pyproject[all]' pyproject.toml
70+
pipx run doc8 --max-line-length=200 --ignore-path=docs/source/other/full-config.rst
71+
6072
check_release:
6173
runs-on: ubuntu-latest
6274
steps:

.pre-commit-config.yaml

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
ci:
2+
autoupdate_schedule: monthly
3+
14
repos:
25
- repo: https://github.com/pre-commit/pre-commit-hooks
36
rev: v4.4.0
@@ -15,54 +18,25 @@ repos:
1518
- id: check-builtin-literals
1619
- id: trailing-whitespace
1720

18-
- repo: https://github.com/psf/black
19-
rev: 22.10.0
20-
hooks:
21-
- id: black
22-
args: ["--line-length", "100"]
23-
24-
- repo: https://github.com/PyCQA/isort
25-
rev: 5.10.1
26-
hooks:
27-
- id: isort
28-
files: \.py$
29-
args: [--profile=black]
30-
stages: [manual]
31-
32-
- repo: https://github.com/abravalheri/validate-pyproject
33-
rev: v0.10.1
21+
- repo: https://github.com/python-jsonschema/check-jsonschema
22+
rev: 0.19.2
3423
hooks:
35-
- id: validate-pyproject
36-
stages: [manual]
24+
- id: check-github-workflows
3725

3826
- repo: https://github.com/executablebooks/mdformat
3927
rev: 0.7.16
4028
hooks:
4129
- id: mdformat
30+
additional_dependencies:
31+
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]
4232

43-
- repo: https://github.com/asottile/pyupgrade
44-
rev: v3.3.0
45-
hooks:
46-
- id: pyupgrade
47-
args: [--py37-plus]
48-
49-
- repo: https://github.com/PyCQA/doc8
50-
rev: v1.0.0
51-
hooks:
52-
- id: doc8
53-
args: [--max-line-length=200]
54-
stages: [manual]
55-
56-
- repo: https://github.com/john-hen/Flake8-pyproject
57-
rev: 1.2.2
33+
- repo: https://github.com/psf/black
34+
rev: 22.10.0
5835
hooks:
59-
- id: Flake8-pyproject
60-
alias: flake8
61-
additional_dependencies:
62-
["flake8-bugbear==22.6.22", "flake8-implicit-str-concat==0.2.0"]
63-
stages: [manual]
36+
- id: black
6437

65-
- repo: https://github.com/python-jsonschema/check-jsonschema
66-
rev: 0.19.2
38+
- repo: https://github.com/charliermarsh/ruff-pre-commit
39+
rev: v0.0.181
6740
hooks:
68-
- id: check-github-workflows
41+
- id: ruff
42+
args: ["--fix"]

check_requirements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
if errors:
2323
print('Missing deps in "all" reqs:')
24-
print([e for e in errors])
24+
print(list(errors))
2525

2626
if remaining_all:
2727
print('Reqs in "all" but nowhere else:')
28-
print([r for r in remaining_all])
28+
print(list(remaining_all))
2929

3030
if errors or remaining_all:
3131
sys.exit(1)

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# Automatically generate config_options.rst
2626
with open(os.path.join(HERE, "..", "autogen_config.py")) as f:
27-
exec(compile(f.read(), "autogen_config.py", "exec"), {})
27+
exec(compile(f.read(), "autogen_config.py", "exec"), {}) # noqa
2828
print("Created docs for config options")
2929

3030
# -- General configuration ------------------------------------------------
@@ -82,7 +82,7 @@
8282
# Get information from _version.py and use it to generate version and release
8383
_version_py = os.path.join(HERE, "../../nbconvert/_version.py")
8484
version_ns = {}
85-
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns)
85+
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns) # noqa
8686
# The short X.Y version.
8787
version = "%i.%i" % version_ns["version_info"][:2]
8888
# The full version, including alpha/beta/rc tags.

docs/source/nbconvert_library.ipynb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"outputs": [],
7272
"source": [
7373
"import nbformat\n",
74+
"\n",
7475
"jake_notebook = nbformat.reads(response, as_version=4)\n",
7576
"jake_notebook.cells[0]"
7677
]
@@ -101,7 +102,7 @@
101102
"\n",
102103
"# 2. Instantiate the exporter. We use the `classic` template for now; we'll get into more details\n",
103104
"# later about how to customize the exporter further.\n",
104-
"html_exporter = HTMLExporter(template_name = 'classic')\n",
105+
"html_exporter = HTMLExporter(template_name='classic')\n",
105106
"\n",
106107
"# 3. Process the notebook we loaded earlier\n",
107108
"(body, resources) = html_exporter.from_notebook_node(jake_notebook)"
@@ -185,6 +186,7 @@
185186
"source": [
186187
"# Import the RST exproter\n",
187188
"from nbconvert import RSTExporter\n",
189+
"\n",
188190
"# Instantiate it\n",
189191
"rst_exporter = RSTExporter()\n",
190192
"# Convert the notebook to RST format\n",
@@ -237,6 +239,7 @@
237239
"outputs": [],
238240
"source": [
239241
"from IPython.display import Image\n",
242+
"\n",
240243
"Image(data=resources['outputs']['output_3_0.png'], format='png')"
241244
]
242245
},
@@ -297,6 +300,7 @@
297300
"source": [
298301
"# create a configuration object that changes the preprocessors\n",
299302
"from traitlets.config import Config\n",
303+
"\n",
300304
"c = Config()\n",
301305
"c.HTMLExporter.preprocessors = ['nbconvert.preprocessors.ExtractOutputPreprocessor']\n",
302306
"\n",
@@ -322,7 +326,7 @@
322326
},
323327
"outputs": [],
324328
"source": [
325-
"(_, resources) = html_exporter.from_notebook_node(jake_notebook)\n",
329+
"(_, resources) = html_exporter.from_notebook_node(jake_notebook)\n",
326330
"(_, resources_with_fig) = html_exporter_with_figs.from_notebook_node(jake_notebook)\n",
327331
"\n",
328332
"print(\"resources without figures:\")\n",
@@ -380,17 +384,18 @@
380384
"from traitlets import Integer\n",
381385
"from nbconvert.preprocessors import Preprocessor\n",
382386
"\n",
387+
"\n",
383388
"class PelicanSubCell(Preprocessor):\n",
384389
" \"\"\"A Pelican specific preprocessor to remove some of the cells of a notebook\"\"\"\n",
385-
" \n",
390+
"\n",
386391
" # I could also read the cells from nb.metadata.pelican if someone wrote a JS extension,\n",
387-
" # but for now I'll stay with configurable value. \n",
388-
" start = Integer(0, help=\"first cell of notebook to be converted\").tag(config=True)\n",
389-
" end = Integer(-1, help=\"last cell of notebook to be converted\").tag(config=True)\n",
390-
" \n",
392+
" # but for now I'll stay with configurable value.\n",
393+
" start = Integer(0, help=\"first cell of notebook to be converted\").tag(config=True)\n",
394+
" end = Integer(-1, help=\"last cell of notebook to be converted\").tag(config=True)\n",
395+
"\n",
391396
" def preprocess(self, nb, resources):\n",
392397
" self.log.info(\"I'll keep only cells from %d to %d\", self.start, self.end)\n",
393-
" nb.cells = nb.cells[self.start:self.end] \n",
398+
" nb.cells = nb.cells[self.start : self.end]\n",
394399
" return nb, resources"
395400
]
396401
},
@@ -412,7 +417,7 @@
412417
"outputs": [],
413418
"source": [
414419
"# Create a new config object that configures both the new preprocessor, as well as the exporter\n",
415-
"c = Config()\n",
420+
"c = Config()\n",
416421
"c.PelicanSubCell.start = 4\n",
417422
"c.PelicanSubCell.end = 6\n",
418423
"c.RSTExporter.preprocessors = [PelicanSubCell]\n",
@@ -443,14 +448,17 @@
443448
"source": [
444449
"from jinja2 import DictLoader\n",
445450
"\n",
446-
"dl = DictLoader({'footer': \n",
447-
"\"\"\"\n",
451+
"dl = DictLoader(\n",
452+
" {\n",
453+
" 'footer': \"\"\"\n",
448454
"{%- extends 'lab/index.html.j2' -%} \n",
449455
"\n",
450456
"{% block footer %}\n",
451457
"FOOOOOOOOTEEEEER\n",
452458
"{% endblock footer %}\n",
453-
"\"\"\"})\n",
459+
"\"\"\"\n",
460+
" }\n",
461+
")\n",
454462
"\n",
455463
"\n",
456464
"exportHTML = HTMLExporter(extra_loaders=[dl], template_file='footer')\n",

nbconvert/exporters/pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .latex import LatexExporter
1616

1717

18-
class LatexFailed(IOError):
18+
class LatexFailed(IOError): # noqa
1919
"""Exception for failed latex run
2020
2121
Captured latex output is in error.output.

nbconvert/exporters/templateexporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def get_source(self, environment, template):
122122
return self.loader.get_source(environment, template)
123123
except TemplateNotFound:
124124
if template.endswith(self.extension):
125-
raise TemplateNotFound(template)
125+
raise TemplateNotFound(template) from None
126126
return self.loader.get_source(environment, template + self.extension)
127127

128128
def list_templates(self):
@@ -569,9 +569,9 @@ def _template_paths(self, prune=True, root_dirs=None):
569569
def get_compatibility_base_template_conf(cls, name):
570570
# Hard-coded base template confs to use for backwards compatibility for 5.x-only templates
571571
if name == "display_priority":
572-
return dict(base_template="base")
572+
return {"base_template": "base"}
573573
if name == "full":
574-
return dict(base_template="classic", mimetypes={"text/html": True})
574+
return {"base_template": "classic", "mimetypes": {"text/html": True}}
575575

576576
def get_template_names(self):
577577
# finds a list of template names where each successive template name is the base template

nbconvert/exporters/tests/files/notebook2.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"metadata": {},
5656
"outputs": [],
5757
"source": [
58-
"a = np.random.uniform(size=(100,100))"
58+
"a = np.random.uniform(size=(100, 100))"
5959
]
6060
},
6161
{

0 commit comments

Comments
 (0)