Skip to content

Commit

Permalink
Adopt ruff and address lint (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Dec 13, 2022
1 parent e5dd908 commit a6c1ee9
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 143 deletions.
6 changes: 4 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
version: 2
updates:
# Set update schedule for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every weekday
interval: "weekly"
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
14 changes: 10 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ jobs:
codecov
- run: hatch version 100.100.100

pre_commit:
runs-on: ubuntu-20.04
test_lint:
name: Test Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- uses: jupyterlab/maintainer-tools/.github/actions/pre-commit@v1
- name: Run Linters
run: |
hatch run typing:test
hatch run lint:style
pipx run 'validate-pyproject[all]' pyproject.toml
pipx run doc8 --max-line-length=200
docs:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -142,7 +148,7 @@ jobs:
if: always()
needs:
- tests
- pre_commit
- test_lint
- docs
- test_minimum_versions
- test_prereleases
Expand Down
59 changes: 13 additions & 46 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ci:
autoupdate_schedule: monthly

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -15,59 +18,23 @@ repos:
- id: check-builtin-literals
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
files: \.py$

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
- id: mypy
additional_dependencies: [types-requests, traitlets, jupyter_core]
stages: [manual]

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.10.1
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.19.2
hooks:
- id: validate-pyproject
stages: [manual]
- id: check-github-workflows

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
hooks:
- id: mdformat

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.0
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/PyCQA/doc8
rev: v1.0.0
hooks:
- id: doc8
args: [--max-line-length=200]
exclude: docs/source/other/full-config.rst
stages: [manual]

- repo: https://github.com/john-hen/Flake8-pyproject
rev: 1.2.2
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: Flake8-pyproject
alias: flake8
additional_dependencies:
["flake8-bugbear==22.6.22", "flake8-implicit-str-concat==0.2.0"]
stages: [manual]
- id: black

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.19.2
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.165
hooks:
- id: check-github-workflows
- id: ruff
args: ["--fix"]
2 changes: 1 addition & 1 deletion nbformat/_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def import_item(name):
try:
pak = getattr(module, obj)
except AttributeError:
raise ImportError("No module named %s" % obj)
raise ImportError("No module named %s" % obj) from None
return pak
else:
# called with un-dotted string
Expand Down
4 changes: 2 additions & 2 deletions nbformat/_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __setattr__(self, key, value):
try:
self.__setitem__(key, value)
except KeyError as e:
raise AttributeError(e)
raise AttributeError(e) from None

def __getattr__(self, key):
"""Get an attr by calling :meth:`dict.__getitem__`.
Expand All @@ -127,7 +127,7 @@ def __getattr__(self, key):
try:
result = self[key]
except KeyError:
raise AttributeError(key)
raise AttributeError(key) from None
else:
return result

Expand Down
2 changes: 1 addition & 1 deletion nbformat/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def convert(nb, to_version):
except AttributeError as e:
raise ValidationError(
f"Notebook could not be converted from version {version} to version {step_version} because it's missing a key: {e}"
)
) from None

# Recursively convert until target version is reached.
return convert(converted, to_version)
Expand Down
2 changes: 1 addition & 1 deletion nbformat/json_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def validate(self, data):
try:
self._validator(data)
except _JsonSchemaException as error:
raise ValidationError(str(error), schema_path=error.path)
raise ValidationError(str(error), schema_path=error.path) from error

def iter_errors(self, data, schema=None):
if schema is not None:
Expand Down
4 changes: 3 additions & 1 deletion nbformat/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def reads(s, **kwargs):
try:
return versions[major].to_notebook_json(nb_dict, minor=minor)
except AttributeError as e:
raise ValidationError(f"The notebook is invalid and is missing an expected key: {e}")
raise ValidationError(
f"The notebook is invalid and is missing an expected key: {e}"
) from None
else:
raise NBFormatError("Unsupported nbformat version %s" % major)

Expand Down
19 changes: 4 additions & 15 deletions nbformat/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@
from base64 import encodebytes

from jupyter_core.application import JupyterApp, base_flags
from traitlets import (
Any,
Bool,
Bytes,
Callable,
Enum,
Instance,
Integer,
Unicode,
default,
observe,
)
from traitlets import Any, Bool, Bytes, Callable, Enum, Instance, Integer, Unicode, default, observe
from traitlets.config import LoggingConfigurable, MultipleInstanceError

from . import NO_CONVERT, __version__, read, reads
Expand Down Expand Up @@ -139,9 +128,9 @@ def close(self):
self.db.close()

def _connect_db(self, db_file):
kwargs: t.Dict[str, t.Any] = dict(
detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
)
kwargs: t.Dict[str, t.Any] = {
"detect_types": sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
}
db = None
try:
db = sqlite3.connect(db_file, **kwargs)
Expand Down
1 change: 1 addition & 0 deletions nbformat/v1/nbbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from .._struct import Struct


# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions nbformat/v1/nbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .nbbase import from_dict
from .rwbase import NotebookReader, NotebookWriter


# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions nbformat/v2/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .nbbase import new_code_cell, new_notebook, new_text_cell, new_worksheet


# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions nbformat/v2/nbbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from .._struct import Struct


# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
Expand Down
9 changes: 2 additions & 7 deletions nbformat/v2/nbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
import json

from .nbbase import from_dict
from .rwbase import (
NotebookReader,
NotebookWriter,
rejoin_lines,
restore_bytes,
split_lines,
)
from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, restore_bytes, split_lines


# -----------------------------------------------------------------------------
# Code
Expand Down
1 change: 1 addition & 0 deletions nbformat/v2/rwbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from base64 import decodebytes, encodebytes


# -----------------------------------------------------------------------------
# Code
# -----------------------------------------------------------------------------
Expand Down
8 changes: 1 addition & 7 deletions nbformat/v3/nbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
import json

from .nbbase import from_dict
from .rwbase import (
NotebookReader,
NotebookWriter,
rejoin_lines,
split_lines,
strip_transient,
)
from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient


class BytesEncoder(json.JSONEncoder):
Expand Down
8 changes: 1 addition & 7 deletions nbformat/v4/nbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
import json

from ..notebooknode import from_dict
from .rwbase import (
NotebookReader,
NotebookWriter,
rejoin_lines,
split_lines,
strip_transient,
)
from .rwbase import NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient


class BytesEncoder(json.JSONEncoder):
Expand Down
6 changes: 3 additions & 3 deletions nbformat/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,14 @@ def validate(
version : int
version_minor : int
relax_add_props : bool
Deprecated since 5.5.0 will be removed in the future.
Deprecated since 5.5.0 - will be removed in the future.
Wether to allow extra property in the Json schema validating the
notebook.
nbjson
repair_duplicate_cell_ids : boolny
Deprecated since 5.5.0 will be removed in the future.
Deprecated since 5.5.0 - will be removed in the future.
strip_invalid_metadata : bool
Deprecated since 5.5.0 will be removed in the future.
Deprecated since 5.5.0 - will be removed in the future.
Returns
-------
Expand Down
Loading

0 comments on commit a6c1ee9

Please sign in to comment.