Skip to content

Commit cc939ba

Browse files
Fix pytest 7 warning (#75)
1 parent 214162c commit cc939ba

File tree

6 files changed

+95
-76
lines changed

6 files changed

+95
-76
lines changed

poetry.lock

Lines changed: 22 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Pygments = "^2.7.3"
2323
ipykernel = ">=5.4.0"
2424

2525
[tool.poetry.dev-dependencies]
26-
pytest = "6.2.5"
26+
pytest = "^7.1.0"
2727
pre-commit = "^2.8.2"
2828
pytest-cov = "^2.10.1"
2929
pytest-xdist = "^2.1.0"

src/nbmake/pytest_plugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pathlib import Path
44
from typing import Any, Optional
55

6+
import pkg_resources
7+
68
from .pytest_items import NotebookFile
79

810

@@ -45,6 +47,11 @@ def pytest_collect_file(path: str, parent: Any) -> Optional[Any]:
4547
opt = parent.config.option
4648
p = Path(path)
4749
if opt.nbmake and p.match("*ipynb") and "_build" not in p.parts:
48-
return NotebookFile.from_parent(parent, fspath=path)
50+
ver: int = int(pkg_resources.get_distribution("pytest").version[0])
51+
52+
if ver < 7:
53+
return NotebookFile.from_parent(parent, fspath=path)
54+
55+
return NotebookFile.from_parent(parent, path=p)
4956

5057
return None

tests/helper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from typing import Any, Dict, List
44

55
import yaml
6-
from _pytest.pytester import Testdir
76
from nbformat import write
87
from nbformat.v4 import new_code_cell, new_markdown_cell, new_notebook
9-
from pytest import fixture
8+
from pytest import Pytester, fixture
109

1110
HOME: str = os.environ["HOME"]
1211

@@ -49,8 +48,7 @@ def write_config(conf: Dict[Any, Any], filename: Path = Path("_config.yml")) ->
4948

5049

5150
@fixture
52-
def testdir2(testdir: Testdir) -> Testdir:
51+
def testdir2(pytester: Pytester):
5352
os.environ[
5453
"HOME"
5554
] = HOME # ensures jupyter client can start the ipykernel subprocess without module location issues
56-
return testdir

tests/test_nb_run.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import os
12
from pathlib import Path
23

3-
from _pytest.pytester import Testdir
44
from nbformat import write
55
from nbformat.v4 import new_code_cell, new_notebook, new_output
6+
from pytest import Pytester
7+
from typing_extensions import Never
68

79
NB_VERSION = 4
810
from nbmake.nb_result import NotebookResult
@@ -16,17 +18,17 @@
1618

1719

1820
class TestNotebookRun:
19-
def test_when_passing_then_no_failing_cell(self, testdir2: Testdir):
21+
def test_when_passing_then_no_failing_cell(self, testdir2: Never):
2022
write_nb(passing_nb, filename)
2123

2224
run = NotebookRun(filename, 300)
2325
res: NotebookResult = run.execute()
2426

2527
assert res.error == None
2628

27-
def test_when_runs_then_cwd_is_nb_location(self, testdir2: Testdir):
29+
def test_when_runs_then_cwd_is_nb_location(self, testdir2: Never):
2830
subdir = Path("subdir")
29-
subdir.mkdir()
31+
subdir.mkdir(exist_ok=True)
3032
write_nb(
3133
["import os; assert os.getcwd().endswith('subdir')"], subdir / filename
3234
)
@@ -36,14 +38,14 @@ def test_when_runs_then_cwd_is_nb_location(self, testdir2: Testdir):
3638

3739
assert res.error == None
3840

39-
def test_failing(self, testdir2: Testdir):
41+
def test_failing(self, testdir2: Never):
4042
write_nb(failing_nb, filename)
4143
run = NotebookRun(filename, 300)
4244
res: NotebookResult = run.execute()
4345

4446
assert res.error and res.error.failing_cell_index == 1
4547

46-
def test_when_allow_errors_then_passing(self, testdir2: Testdir):
48+
def test_when_allow_errors_then_passing(self, testdir2: Never):
4749
nb = new_notebook()
4850
nb.metadata.execution = {"allow_errors": True}
4951
cell = new_code_cell("raise Exception()")
@@ -56,7 +58,7 @@ def test_when_allow_errors_then_passing(self, testdir2: Testdir):
5658
assert not res.error
5759
assert res.nb.cells[0].outputs[0].ename == "Exception"
5860

59-
def test_when_timeout_then_fails(self, testdir2: Testdir):
61+
def test_when_timeout_then_fails(self, testdir2: Never):
6062
nb = new_notebook()
6163
nb.metadata.execution = {"timeout": 1}
6264
nb.cells += [
@@ -72,7 +74,7 @@ def test_when_timeout_then_fails(self, testdir2: Testdir):
7274
assert not Path("fail.txt").exists()
7375
assert res.error and res.error.failing_cell_index == 1
7476

75-
def test_when_executed_then_stripped_out(self, testdir2: Testdir):
77+
def test_when_executed_then_stripped_out(self, testdir2: Never):
7678
nb = new_notebook(metadata={})
7779
nb.cells += [
7880
new_code_cell(
@@ -91,7 +93,7 @@ def test_when_executed_then_stripped_out(self, testdir2: Testdir):
9193
assert res.nb.cells[0].outputs[0].ename == "Exception"
9294
assert res.nb.cells[1].outputs == []
9395

94-
def test_when_unknown_kernel_then_error(self, testdir2: Testdir):
96+
def test_when_unknown_kernel_then_error(self, testdir2: Never):
9597
nb = new_notebook(
9698
metadata={"kernelspec": {"display_name": "asdf", "name": "asdf"}}
9799
)
@@ -102,19 +104,19 @@ def test_when_unknown_kernel_then_error(self, testdir2: Testdir):
102104
res: NotebookResult = run.execute()
103105
assert res.error and "No such kernel" in res.error.summary
104106

105-
def test_when_cell_ignored_then_does_not_run(self, testdir2: Testdir):
107+
def test_when_cell_ignored_then_does_not_run(self, testdir2: Never):
106108
nb = Path(__file__).parent / "resources" / "ignore_tag.ipynb"
107109
run = NotebookRun(nb, 300)
108110
res: NotebookResult = run.execute()
109111
assert res.error == None
110112

111-
def test_when_raises_exc_tag_then_succeeds(self, testdir2: Testdir):
113+
def test_when_raises_exc_tag_then_succeeds(self, testdir2: Never):
112114
nb = Path(__file__).parent / "resources" / "raises_tag.ipynb"
113115
run = NotebookRun(nb, 300)
114116
res: NotebookResult = run.execute()
115117
assert res.error == None
116118

117-
def test_when_mock_then_succeeds(self, testdir2: Testdir):
119+
def test_when_mock_then_succeeds(self, testdir2: Never):
118120
nb = Path(__file__).parent / "resources" / "mock.ipynb"
119121
run = NotebookRun(nb, 300)
120122
res: NotebookResult = run.execute()

0 commit comments

Comments
 (0)