diff --git a/papyri/gen.py b/papyri/gen.py index a0424f20..f5658e76 100644 --- a/papyri/gen.py +++ b/papyri/gen.py @@ -30,7 +30,17 @@ from itertools import count from pathlib import Path from types import FunctionType, ModuleType -from typing import Any, Dict, FrozenSet, List, MutableMapping, Optional, Sequence, Tuple +from typing import ( + Any, + Dict, + FrozenSet, + List, + MutableMapping, + Optional, + Sequence, + Tuple, + Union, +) import io import jedi @@ -1134,16 +1144,64 @@ def report_unexpected_exception(self, out, test, example, exc_info): ) def report_failure(self, out, test, example, got): + out("Got test", repr(got), repr(test)) tok_entries = self._get_tok_entries(example) self._example_section_data.append( Code(tok_entries, got, ExecutionStatus.failure) ) - def get_example_section_data(self): + def get_example_section_data(self) -> Section: example_section_data = self._example_section_data self._example_section_data = Section([], None) return example_section_data + # TODO: + + def _compact(self, example_section_data) -> Section: + acc: List[Union[MText, Code]] = [] + current_code: Optional[ + Code + ] = None # a code item which never have am out, or None + + # class GenToken(Node): + # value: str + # qa: Optional[str] + # pygmentclass: str + + # class Code(Node): + # entries: List[GenToken] + # out: str + # ce_status: str + + for item in example_section_data: + if not isinstance(item, Code): + if current_code is not None: + acc.append(current_code) + acc.append(MText(str(current_code.out))) + acc.append(MText(str(current_code.ce_status))) + current_code = None + acc.append(item) + else: + if current_code is None: + assert item is not None + current_code = item + continue + + if current_code.ce_status == item.ce_status: + current_code = Code( + current_code.entries + item.entries, item.out, item.ce_status + ) + else: + acc.append(current_code) + acc.append(MText(str(current_code.out))) + acc.append(MText(str(current_code.ce_status))) + assert item is not None + current_code = item + + if current_code: + acc.append(current_code) + return Section(acc, None) + class Gen: """ @@ -1347,6 +1405,10 @@ def debugprint(*args): elif block: example_section_data.append(MText(block)) + example_section_data = doctest_runner._compact(example_section_data) + + # TODO: compact codeblocks here, if we have multiple codebalc + # TODO fix this if plt.close not called and still a lingering figure. fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() if len(fig_managers) != 0: