Skip to content

Commit

Permalink
Ensure collector nodeid is assigned consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
lbianchi-lbl committed Mar 29, 2023
1 parent 84b8113 commit 464937a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions nbcheck/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def from_path(cls, path: Path, session: pytest.Session):
relpath = bestrelpath(session.config.rootpath, path)

return cls.from_parent(
parent=session,
session,
name=relpath,
nodeid=relpath,
)
Expand All @@ -43,7 +43,7 @@ def from_path(cls,
)

return cls.from_parent(
parent=parent,
parent,
name=path.name,
nb=nb,
path=path,
Expand Down
8 changes: 4 additions & 4 deletions nbcheck/cell_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def from_code_cells(cls, code_cells: Iterable[Cell], parent: pytest.Collector):
for expected_index, cell in enumerate(code_cells, start=1):
name = f"[{expected_index}]"
yield cls.from_parent(
parent=parent,
parent,
name=name,
cell=cell,
expected=expected_index,
Expand Down Expand Up @@ -70,7 +70,7 @@ def collect(self):
for expected_index, cell in enumerate(self.cells, start=1):
name = f"[{expected_index}]"
yield CellIndexCheck.from_parent(
parent=self,
self,
name=name,
cell=cell,
expected=expected_index,
Expand All @@ -80,9 +80,9 @@ def collect(self):
def pytest_nbcollect_makeitem(collector: Notebook):
cells = collector.code_cells
return CellOrder.from_parent(
parent=collector,
collector,
cells=cells,
name=f"(code) 1-{len(cells)}"
name=f"cell_order (1-{len(cells)})"
)


Expand Down
13 changes: 12 additions & 1 deletion nbcheck/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def pytest_nbcheck_info(config: pytest.Config) -> List[str]:


class CellsExec(nbval_plugin.IPyNbFile):
def __init__(self, *, name=None, nodeid=None, parent=None, **kwargs):
if nodeid is None and parent is not None and name is not None:
# this is to ensure a consistent behavior with the sub-collectors
# (i.e. whose parent is a nbcheck.api.Notebook) from the other nbcheck plugins
# since they're not subclasses of pytest.FSCollector
# It looks like if nodeid is not set explicitly and path == self.parent.path,
# the nodeid will be mangled with extra :: to ensure uniqueness
nodeid = f"{parent.nodeid}::{name}"
super().__init__(name=name, nodeid=nodeid, parent=parent, **kwargs)

def collect(self):
# overwrite the default 0-based cell_num
orig_items = super().collect()
Expand All @@ -82,8 +92,9 @@ def collect(self):
@pytest.hookimpl(trylast=True)
def pytest_nbcollect_makeitem(collector: Notebook):
return CellsExec.from_parent(
parent=collector,
collector,
path=collector.path,
name="exec (nbval)"
)


Expand Down
2 changes: 1 addition & 1 deletion nbcheck/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def reportinfo(self):
def pytest_nbcollect_makeitem(collector: Notebook):
headers = Headers.from_markdown_cells(collector.markdown_cells)
return HeaderStructure.from_parent(
parent=collector,
collector,
name=f"headers ({len(headers)})",
headers=headers,
)
Expand Down

0 comments on commit 464937a

Please sign in to comment.