Skip to content

Commit

Permalink
🔧 Pre-commit; replace black,isort with ruff-fmt (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Nov 28, 2023
1 parent ca71405 commit 7a43086
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 49 deletions.
16 changes: 4 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ repos:
- id: trailing-whitespace
exclude: ^tests/.*\.txt

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

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

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.5
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
1 change: 0 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# pytest-notebook documentation build configuration file
#
Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ coalesce_streams = "pytest_notebook.post_processors:coalesce_streams"
blacken_code = "pytest_notebook.post_processors:blacken_code"
beautifulsoup = "pytest_notebook.post_processors:beautifulsoup"

[tool.isort]
profile = "black"
force_sort_within_sections = true

[tool.ruff]
line-length = 100
extend-select = ["B0", "C4", "ICN", "ISC", "N", "RUF", "SIM"]
extend-select = ["B0", "C4", "I", "ICN", "ISC", "N", "RUF", "SIM", "UP"]
extend-ignore = ["ISC001"]

[tool.ruff.lint.isort]
force-sort-within-sections = true
8 changes: 4 additions & 4 deletions pytest_notebook/ipy_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse_cell_content(
if in_config:
in_config = False
elif config_content:
raise IOError(f"Line {i}: found second config file section")
raise OSError(f"Line {i}: found second config file section")
else:
in_config = True
elif in_config:
Expand All @@ -48,7 +48,7 @@ def parse_cell_content(
if in_literals:
in_literals = False
elif literals_content:
raise IOError(f"Line {i}: found second literals section")
raise OSError(f"Line {i}: found second literals section")
else:
in_literals = True
elif in_literals:
Expand All @@ -57,9 +57,9 @@ def parse_cell_content(
test_content.append(line)

if in_config:
raise IOError("found start of config section, but not end")
raise OSError("found start of config section, but not end")
if in_literals:
raise IOError("found start of literals section, but not end")
raise OSError("found start of literals section, but not end")

return test_content, config_content, literals_content

Expand Down
4 changes: 2 additions & 2 deletions pytest_notebook/nb_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _validate_exec_cwd(self, attribute, value):
if not isinstance(value, str):
raise TypeError("exec_cwd must be None or a string")
if not os.path.isdir(value):
raise IOError("exec_cwd='{}' is not an existing directory".format(value))
raise OSError(f"exec_cwd='{value}' is not an existing directory")

exec_allow_errors: bool = attr.ib(
False, instance_of(bool), metadata={"help": HELP_EXEC_ALLOW_ERRORS}
Expand Down Expand Up @@ -248,7 +248,7 @@ def __setattr__(self, key, value):
if x_attr.validator:
x_attr.validator(self, x_attr, value)

super(NBRegressionFixture, self).__setattr__(key, value)
super().__setattr__(key, value)

def check(
self, path: Union[TextIO, str], raise_errors: bool = True
Expand Down
12 changes: 9 additions & 3 deletions pytest_notebook/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ def gather_json_paths(
if isinstance(obj, dict):
for k, v in obj.items():
gather_json_paths(
v, paths, types, curr_path=tuple(list(curr_path) + [k]) # noqa: RUF005
v,
paths,
types,
curr_path=tuple(list(curr_path) + [k]), # noqa: RUF005
)
elif isinstance(obj, list):
for i, v in enumerate(obj):
gather_json_paths(
v, paths, types, curr_path=tuple(list(curr_path) + [i]) # noqa: RUF005
v,
paths,
types,
curr_path=tuple(list(curr_path) + [i]), # noqa: RUF005
)
elif types is None or isinstance(obj, types):
paths.append(curr_path)
Expand Down Expand Up @@ -149,7 +155,7 @@ def validate_regex_replace(args, index):
raise TypeError(f"diff_replace[{index}] replacement '{args[2]}' must a string")


@lru_cache()
@lru_cache
def _load_validator():
schema = json.loads(
files(resources).joinpath("nb_metadata.schema.json").read_text(encoding="utf-8")
Expand Down
1 change: 0 additions & 1 deletion pytest_notebook/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""pytest plugin configuration.
For more information on writing pytest plugins see:
Expand Down
6 changes: 3 additions & 3 deletions pytest_notebook/post_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
ENTRY_POINT_NAME = "nbreg.post_proc"


@functools.lru_cache()
@functools.lru_cache
def list_processor_names():
"""List entry point names for post-processors."""
return [ep.name for ep in entry_points().select(group=ENTRY_POINT_NAME)]


@functools.lru_cache()
@functools.lru_cache
def load_processor(name: str):
"""Get a post-processors for an entry point name."""
try:
(entry_point,) = entry_points().select(group=ENTRY_POINT_NAME, name=name)
except ValueError:
raise ValueError(
"entry point '{}' for group '{}' not found".format(name, ENTRY_POINT_NAME)
f"entry point '{name}' for group '{ENTRY_POINT_NAME}' not found"
)
return entry_point.load()

Expand Down
9 changes: 3 additions & 6 deletions pytest_notebook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,12 @@ def param_doc(field):
return title + "\n" + description

if attr.fields(attrs_class):
params_section = (
textwrap.dedent(
"""\
params_section = textwrap.dedent(
"""\
Parameters
----------
"""
)
+ "\n\n".join(param_doc(field) for field in attr.fields(attrs_class))
)
) + "\n\n".join(param_doc(field) for field in attr.fields(attrs_class))
else:
params_section = ""

Expand Down
1 change: 0 additions & 1 deletion tests/test_plugin_collector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Test the plugin collection and direct invocation of notebooks."""
import os

Expand Down
13 changes: 3 additions & 10 deletions tests/test_plugin_fixture.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Test the ``nb_regression`` plugin fixture."""
import os

Expand Down Expand Up @@ -105,9 +104,7 @@ def test_nb_regression_ini_setting_init(testdir):
nb_post_processors =
nb_diff_replace =
/cells/*/outputs/*/traceback \<ipython\-input\-[\-0-9a-zA-Z]*\> "< >"
""".format(
path=os.path.join(PATH, "raw_files")
)
""".format(path=os.path.join(PATH, "raw_files"))
)

testdir.makepyfile(
Expand Down Expand Up @@ -167,9 +164,7 @@ def test_nb_regression_fixture_check_fail(testdir):
"""
def test_nb(nb_regression):
nb_regression.check("{path}")
""".format(
path=os.path.join(PATH, "raw_files", "simple-diff-output.ipynb")
)
""".format(path=os.path.join(PATH, "raw_files", "simple-diff-output.ipynb"))
)

# run pytest with the following cmd args
Expand Down Expand Up @@ -198,9 +193,7 @@ def test_nb(nb_regression):
"/cells/9/outputs/0/metadata/application/json"
)
nb_regression.check("{path}")
""".format(
path=os.path.join(PATH, "raw_files", "different_outputs.ipynb")
)
""".format(path=os.path.join(PATH, "raw_files", "different_outputs.ipynb"))
)

# run pytest with the following cmd args
Expand Down

0 comments on commit 7a43086

Please sign in to comment.