Skip to content

Commit

Permalink
switch to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-treebeard committed Dec 23, 2024
1 parent 1b9f787 commit 9f269b4
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 47 deletions.
31 changes: 29 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ virtualenv = "^20.1.0"
setuptools = ">65.0.0"
typing-extensions = "^4.7.1"

[tool.poetry.group.dev.dependencies]
ruff = "^0.8.4"

[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["setuptools", "poetry-core>=1.0.0"]
10 changes: 3 additions & 7 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[lint]

# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
select = ["E", "F", "I"]
ignore = []
Expand All @@ -6,14 +8,8 @@ ignore = []
fixable = ["A", "B", "C", "D", "E", "F", "I"]
unfixable = []

# Same as Black.
line-length = 88

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Assume Python 3.8
target-version = "py38"

[isort]
[lint.isort]
known-first-party = ["nbmake"]
2 changes: 1 addition & 1 deletion src/nbmake/nb_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async def apply_mocks(
except Exception as err:
# if at top causes https://github.com/jupyter/nbclient/issues/128
# from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel
# trace=f"{summary}\n\nInstalled Kernels: {str(KernelSpecManager().find_kernel_specs())}",
# trace=f"{summary}\n\nInstalled Kernels: {str(KernelSpecManager().find_kernel_specs())}", # noqa
# https://github.com/treebeardtech/nbmake/runs/1536896858?check_suite_focus=true
if str(type(err)) != "<class 'jupyter_client.kernelspec.NoSuchKernel'>":
raise err
Expand Down
2 changes: 1 addition & 1 deletion src/nbmake/pytest_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def runtest(self):
if option.overwrite:
nbformat.write(res.nb, str(source))

if res.error != None:
if res.error is not None:
raise NotebookFailedException(res)

def repr_failure(self, excinfo: Any, style: Optional[Any] = None) -> TerminalRepr:
Expand Down
6 changes: 3 additions & 3 deletions src/nbmake/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def pytest_terminal_summary(terminalreporter: Any, exitstatus: int, config: Any)
if os.environ.get("GITHUB_ACTIONS", False):
if exitstatus == ExitCode.TESTS_FAILED:
print(
f"\n* nbmake: Automate reading GitHub Actions logs with our bot: https://github.com/marketplace/treebeard-build\n"
"\n* nbmake: Automate reading GitHub Actions logs with our bot: https://github.com/marketplace/treebeard-build\n"
)
else:
print(
f"\nLearn more about nbmake at https://github.com/treebeardtech/nbmake\n"
"\nLearn more about nbmake at https://github.com/treebeardtech/nbmake\n"
)
except:
except: # noqa
pass
2 changes: 1 addition & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def write_nb(
def testdir2(pytester: Pytester):
os.environ[
"HOME"
] = HOME # ensures jupyter client can start the ipykernel subprocess without module location issues
] = HOME # ensures jupyter client can start the ipykernel subprocess without module location issues # noqa
4 changes: 2 additions & 2 deletions tests/resources/ignore_tag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"outputs": [],
"source": [
"print('will fail')\n",
"1/0"
"print(\"will fail\")\n",
"1 / 0"
]
},
{
Expand Down
13 changes: 4 additions & 9 deletions tests/resources/import_errs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"import itertools\n",
"\n",
"1/0"
"1 / 0"
]
},
{
Expand All @@ -18,6 +16,7 @@
"outputs": [],
"source": [
"import time\n",
"\n",
"time.sleep(600)"
]
},
Expand All @@ -26,18 +25,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import lkjlkj"
]
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle"
]
"source": []
}
],
"metadata": {
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/mock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"outputs": [],
"source": [
"x = 5\n",
"y = 'y'"
"y = \"y\""
]
},
{
Expand All @@ -28,7 +28,7 @@
"source": [
"assert x == 2\n",
"assert y == \"fish\"\n",
"assert z == { \"x\": 42 }"
"assert z == {\"x\": 42} # noqa"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/raises_tag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"outputs": [],
"source": [
"print('will fail')\n",
"1/0"
"print(\"will fail\")\n",
"1 / 0"
]
},
{
Expand Down
22 changes: 10 additions & 12 deletions tests/test_nb_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import pytest
from nbformat import write
from nbformat.v4 import new_code_cell, new_notebook, new_output
from pytest import Pytester
from typing_extensions import Never

NB_VERSION = 4
from nbmake.nb_result import NotebookResult
from nbmake.nb_run import NotebookRun

from .helper import failing_nb, passing_nb, testdir2, write_nb
from .helper import failing_nb, passing_nb, write_nb

pytest_plugins = "pytester"

Expand All @@ -24,7 +22,7 @@ def test_when_passing_then_no_failing_cell(self, testdir2: Never):
run = NotebookRun(filename, 300)
res: NotebookResult = run.execute()

assert res.error == None
assert res.error is None

def test_when_runs_then_cwd_is_nb_location(self, testdir2: Never):
subdir = Path("subdir")
Expand All @@ -36,7 +34,7 @@ def test_when_runs_then_cwd_is_nb_location(self, testdir2: Never):
run = NotebookRun(subdir / filename, 300)
res: NotebookResult = run.execute()

assert res.error == None
assert res.error is None

def test_failing(self, testdir2: Never):
write_nb(failing_nb, filename)
Expand Down Expand Up @@ -108,33 +106,33 @@ def test_when_cell_ignored_then_does_not_run(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "ignore_tag.ipynb"
run = NotebookRun(nb, 300)
res: NotebookResult = run.execute()
assert res.error == None
assert res.error is None

def test_when_raises_exc_tag_then_succeeds(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "raises_tag.ipynb"
run = NotebookRun(nb, 300)
res: NotebookResult = run.execute()
assert res.error == None
assert res.error is None

def test_when_mock_then_succeeds(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "mock.ipynb"
run = NotebookRun(nb, 300)
res: NotebookResult = run.execute()
assert res.error == None
assert res.error is None

def test_when_post_cell_execute_then_succeeds(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "post_cell_execute.ipynb"
run = NotebookRun(nb, 300)
res: NotebookResult = run.execute()
assert res.error == None
assert res.error is None

def test_when_post_cell_execute_then_command_fails(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "post_cell_execute_error.ipynb"
run = NotebookRun(nb, 300)
with pytest.raises(Exception) as exc_info:
run.execute()

assert exc_info != None
assert exc_info is not None
assert "boom!" in exc_info.value.args[0]

def test_when_post_cell_execute_then_cell_fails(self, testdir2: Never):
Expand All @@ -146,14 +144,14 @@ def test_when_post_cell_execute_then_cell_fails(self, testdir2: Never):

# make sure the cell exception (bang!) is raised and not masked
# by the post cell execution exception (boom!)
assert res.error != None
assert res.error is not None
assert "bang!" in res.error.summary

def test_when_magic_error_then_fails(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "magic_error.ipynb"
run = NotebookRun(nb, 300)
res: NotebookResult = run.execute()
assert res.error != None
assert res.error is not None

def test_when_empty_then_succeeds(self, testdir2: Never):
nb = Path(__file__).parent / "resources" / "empty.ipynb"
Expand Down
8 changes: 3 additions & 5 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import print_function

import os
from importlib import import_module, invalidate_caches, reload
from importlib import import_module, reload
from pathlib import Path
from unittest.mock import patch

from nbformat import read
from pytest import ExitCode, Pytester
from typing_extensions import Never

from .helper import failing_nb, passing_nb, testdir2, write_nb
from .helper import failing_nb, passing_nb, write_nb

pytest_plugins = "pytester"
NB_VERSION = 4
Expand Down Expand Up @@ -172,7 +171,7 @@ def test_when_explicit_metadata_then_ignore_timeout(
def test_when_kernel_passed_then_override(pytester: Pytester, testdir2: Never):
write_nb(
passing_nb,
Path(f"x.ipynb"),
Path("x.ipynb"),
metadata={
"kernelspec": {
"display_name": "Python 3",
Expand All @@ -199,7 +198,6 @@ def test_when_no_import_errs_then_pass(pytester: Pytester, testdir2: Never):


def test_when_not_json_then_correct_err_msg(pytester: Pytester, testdir2: Never):

(Path(pytester.path) / "a.ipynb").write_text("invalid json")

hook_recorder = pytester.inline_run("--nbmake")
Expand Down

0 comments on commit 9f269b4

Please sign in to comment.