pytest_ipdoctest.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. # Based on Pytest doctest.py
  2. # Original license:
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) 2004-2021 Holger Krekel and others
  6. """Discover and run ipdoctests in modules and test files."""
  7. import bdb
  8. import builtins
  9. import inspect
  10. import os
  11. import platform
  12. import sys
  13. import traceback
  14. import types
  15. import warnings
  16. from contextlib import contextmanager
  17. from pathlib import Path
  18. from typing import (
  19. TYPE_CHECKING,
  20. Any,
  21. Callable,
  22. Dict,
  23. Generator,
  24. Iterable,
  25. List,
  26. Optional,
  27. Pattern,
  28. Sequence,
  29. Tuple,
  30. Type,
  31. Union,
  32. )
  33. import pytest
  34. from _pytest import outcomes
  35. from _pytest._code.code import ExceptionInfo, ReprFileLocation, TerminalRepr
  36. from _pytest._io import TerminalWriter
  37. from _pytest.compat import safe_getattr
  38. from _pytest.config import Config
  39. from _pytest.config.argparsing import Parser
  40. from _pytest.fixtures import FixtureRequest
  41. from _pytest.nodes import Collector
  42. from _pytest.outcomes import OutcomeException
  43. from _pytest.pathlib import fnmatch_ex, import_path
  44. from _pytest.python_api import approx
  45. from _pytest.warning_types import PytestWarning
  46. if TYPE_CHECKING:
  47. import doctest
  48. from .ipdoctest import IPDoctestOutputChecker
  49. DOCTEST_REPORT_CHOICE_NONE = "none"
  50. DOCTEST_REPORT_CHOICE_CDIFF = "cdiff"
  51. DOCTEST_REPORT_CHOICE_NDIFF = "ndiff"
  52. DOCTEST_REPORT_CHOICE_UDIFF = "udiff"
  53. DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE = "only_first_failure"
  54. DOCTEST_REPORT_CHOICES = (
  55. DOCTEST_REPORT_CHOICE_NONE,
  56. DOCTEST_REPORT_CHOICE_CDIFF,
  57. DOCTEST_REPORT_CHOICE_NDIFF,
  58. DOCTEST_REPORT_CHOICE_UDIFF,
  59. DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE,
  60. )
  61. # Lazy definition of runner class
  62. RUNNER_CLASS = None
  63. # Lazy definition of output checker class
  64. CHECKER_CLASS: Optional[Type["IPDoctestOutputChecker"]] = None
  65. def pytest_addoption(parser: Parser) -> None:
  66. parser.addini(
  67. "ipdoctest_optionflags",
  68. "option flags for ipdoctests",
  69. type="args",
  70. default=["ELLIPSIS"],
  71. )
  72. parser.addini(
  73. "ipdoctest_encoding", "encoding used for ipdoctest files", default="utf-8"
  74. )
  75. group = parser.getgroup("collect")
  76. group.addoption(
  77. "--ipdoctest-modules",
  78. action="store_true",
  79. default=False,
  80. help="run ipdoctests in all .py modules",
  81. dest="ipdoctestmodules",
  82. )
  83. group.addoption(
  84. "--ipdoctest-report",
  85. type=str.lower,
  86. default="udiff",
  87. help="choose another output format for diffs on ipdoctest failure",
  88. choices=DOCTEST_REPORT_CHOICES,
  89. dest="ipdoctestreport",
  90. )
  91. group.addoption(
  92. "--ipdoctest-glob",
  93. action="append",
  94. default=[],
  95. metavar="pat",
  96. help="ipdoctests file matching pattern, default: test*.txt",
  97. dest="ipdoctestglob",
  98. )
  99. group.addoption(
  100. "--ipdoctest-ignore-import-errors",
  101. action="store_true",
  102. default=False,
  103. help="ignore ipdoctest ImportErrors",
  104. dest="ipdoctest_ignore_import_errors",
  105. )
  106. group.addoption(
  107. "--ipdoctest-continue-on-failure",
  108. action="store_true",
  109. default=False,
  110. help="for a given ipdoctest, continue to run after the first failure",
  111. dest="ipdoctest_continue_on_failure",
  112. )
  113. def pytest_unconfigure() -> None:
  114. global RUNNER_CLASS
  115. RUNNER_CLASS = None
  116. def pytest_collect_file(
  117. file_path: Path,
  118. parent: Collector,
  119. ) -> Optional[Union["IPDoctestModule", "IPDoctestTextfile"]]:
  120. config = parent.config
  121. if file_path.suffix == ".py":
  122. if config.option.ipdoctestmodules and not any(
  123. (_is_setup_py(file_path), _is_main_py(file_path))
  124. ):
  125. mod: IPDoctestModule = IPDoctestModule.from_parent(parent, path=file_path)
  126. return mod
  127. elif _is_ipdoctest(config, file_path, parent):
  128. txt: IPDoctestTextfile = IPDoctestTextfile.from_parent(parent, path=file_path)
  129. return txt
  130. return None
  131. if int(pytest.__version__.split(".")[0]) < 7:
  132. _collect_file = pytest_collect_file
  133. def pytest_collect_file(
  134. path,
  135. parent: Collector,
  136. ) -> Optional[Union["IPDoctestModule", "IPDoctestTextfile"]]:
  137. return _collect_file(Path(path), parent)
  138. _import_path = import_path
  139. def import_path(path, root):
  140. import py.path
  141. return _import_path(py.path.local(path))
  142. def _is_setup_py(path: Path) -> bool:
  143. if path.name != "setup.py":
  144. return False
  145. contents = path.read_bytes()
  146. return b"setuptools" in contents or b"distutils" in contents
  147. def _is_ipdoctest(config: Config, path: Path, parent: Collector) -> bool:
  148. if path.suffix in (".txt", ".rst") and parent.session.isinitpath(path):
  149. return True
  150. globs = config.getoption("ipdoctestglob") or ["test*.txt"]
  151. return any(fnmatch_ex(glob, path) for glob in globs)
  152. def _is_main_py(path: Path) -> bool:
  153. return path.name == "__main__.py"
  154. class ReprFailDoctest(TerminalRepr):
  155. def __init__(
  156. self, reprlocation_lines: Sequence[Tuple[ReprFileLocation, Sequence[str]]]
  157. ) -> None:
  158. self.reprlocation_lines = reprlocation_lines
  159. def toterminal(self, tw: TerminalWriter) -> None:
  160. for reprlocation, lines in self.reprlocation_lines:
  161. for line in lines:
  162. tw.line(line)
  163. reprlocation.toterminal(tw)
  164. class MultipleDoctestFailures(Exception):
  165. def __init__(self, failures: Sequence["doctest.DocTestFailure"]) -> None:
  166. super().__init__()
  167. self.failures = failures
  168. def _init_runner_class() -> Type["IPDocTestRunner"]:
  169. import doctest
  170. from .ipdoctest import IPDocTestRunner
  171. class PytestDoctestRunner(IPDocTestRunner):
  172. """Runner to collect failures.
  173. Note that the out variable in this case is a list instead of a
  174. stdout-like object.
  175. """
  176. def __init__(
  177. self,
  178. checker: Optional["IPDoctestOutputChecker"] = None,
  179. verbose: Optional[bool] = None,
  180. optionflags: int = 0,
  181. continue_on_failure: bool = True,
  182. ) -> None:
  183. super().__init__(checker=checker, verbose=verbose, optionflags=optionflags)
  184. self.continue_on_failure = continue_on_failure
  185. def report_failure(
  186. self,
  187. out,
  188. test: "doctest.DocTest",
  189. example: "doctest.Example",
  190. got: str,
  191. ) -> None:
  192. failure = doctest.DocTestFailure(test, example, got)
  193. if self.continue_on_failure:
  194. out.append(failure)
  195. else:
  196. raise failure
  197. def report_unexpected_exception(
  198. self,
  199. out,
  200. test: "doctest.DocTest",
  201. example: "doctest.Example",
  202. exc_info: Tuple[Type[BaseException], BaseException, types.TracebackType],
  203. ) -> None:
  204. if isinstance(exc_info[1], OutcomeException):
  205. raise exc_info[1]
  206. if isinstance(exc_info[1], bdb.BdbQuit):
  207. outcomes.exit("Quitting debugger")
  208. failure = doctest.UnexpectedException(test, example, exc_info)
  209. if self.continue_on_failure:
  210. out.append(failure)
  211. else:
  212. raise failure
  213. return PytestDoctestRunner
  214. def _get_runner(
  215. checker: Optional["IPDoctestOutputChecker"] = None,
  216. verbose: Optional[bool] = None,
  217. optionflags: int = 0,
  218. continue_on_failure: bool = True,
  219. ) -> "IPDocTestRunner":
  220. # We need this in order to do a lazy import on doctest
  221. global RUNNER_CLASS
  222. if RUNNER_CLASS is None:
  223. RUNNER_CLASS = _init_runner_class()
  224. # Type ignored because the continue_on_failure argument is only defined on
  225. # PytestDoctestRunner, which is lazily defined so can't be used as a type.
  226. return RUNNER_CLASS( # type: ignore
  227. checker=checker,
  228. verbose=verbose,
  229. optionflags=optionflags,
  230. continue_on_failure=continue_on_failure,
  231. )
  232. class IPDoctestItem(pytest.Item):
  233. _user_ns_orig: Dict[str, Any]
  234. def __init__(
  235. self,
  236. name: str,
  237. parent: "Union[IPDoctestTextfile, IPDoctestModule]",
  238. runner: Optional["IPDocTestRunner"] = None,
  239. dtest: Optional["doctest.DocTest"] = None,
  240. ) -> None:
  241. super().__init__(name, parent)
  242. self.runner = runner
  243. self.dtest = dtest
  244. self.obj = None
  245. self.fixture_request: Optional[FixtureRequest] = None
  246. self._user_ns_orig = {}
  247. @classmethod
  248. def from_parent( # type: ignore
  249. cls,
  250. parent: "Union[IPDoctestTextfile, IPDoctestModule]",
  251. *,
  252. name: str,
  253. runner: "IPDocTestRunner",
  254. dtest: "doctest.DocTest",
  255. ):
  256. # incompatible signature due to imposed limits on subclass
  257. """The public named constructor."""
  258. return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)
  259. def setup(self) -> None:
  260. if self.dtest is not None:
  261. self.fixture_request = _setup_fixtures(self)
  262. globs = dict(getfixture=self.fixture_request.getfixturevalue)
  263. for name, value in self.fixture_request.getfixturevalue(
  264. "ipdoctest_namespace"
  265. ).items():
  266. globs[name] = value
  267. self.dtest.globs.update(globs)
  268. from .ipdoctest import IPExample
  269. if isinstance(self.dtest.examples[0], IPExample):
  270. # for IPython examples *only*, we swap the globals with the ipython
  271. # namespace, after updating it with the globals (which doctest
  272. # fills with the necessary info from the module being tested).
  273. self._user_ns_orig = {}
  274. self._user_ns_orig.update(_ip.user_ns)
  275. _ip.user_ns.update(self.dtest.globs)
  276. # We must remove the _ key in the namespace, so that Python's
  277. # doctest code sets it naturally
  278. _ip.user_ns.pop("_", None)
  279. _ip.user_ns["__builtins__"] = builtins
  280. self.dtest.globs = _ip.user_ns
  281. def teardown(self) -> None:
  282. from .ipdoctest import IPExample
  283. # Undo the test.globs reassignment we made
  284. if isinstance(self.dtest.examples[0], IPExample):
  285. self.dtest.globs = {}
  286. _ip.user_ns.clear()
  287. _ip.user_ns.update(self._user_ns_orig)
  288. del self._user_ns_orig
  289. self.dtest.globs.clear()
  290. def runtest(self) -> None:
  291. assert self.dtest is not None
  292. assert self.runner is not None
  293. _check_all_skipped(self.dtest)
  294. self._disable_output_capturing_for_darwin()
  295. failures: List["doctest.DocTestFailure"] = []
  296. # exec(compile(..., "single", ...), ...) puts result in builtins._
  297. had_underscore_value = hasattr(builtins, "_")
  298. underscore_original_value = getattr(builtins, "_", None)
  299. # Save our current directory and switch out to the one where the
  300. # test was originally created, in case another doctest did a
  301. # directory change. We'll restore this in the finally clause.
  302. curdir = os.getcwd()
  303. os.chdir(self.fspath.dirname)
  304. try:
  305. # Type ignored because we change the type of `out` from what
  306. # ipdoctest expects.
  307. self.runner.run(self.dtest, out=failures, clear_globs=False) # type: ignore[arg-type]
  308. finally:
  309. os.chdir(curdir)
  310. if had_underscore_value:
  311. setattr(builtins, "_", underscore_original_value)
  312. elif hasattr(builtins, "_"):
  313. delattr(builtins, "_")
  314. if failures:
  315. raise MultipleDoctestFailures(failures)
  316. def _disable_output_capturing_for_darwin(self) -> None:
  317. """Disable output capturing. Otherwise, stdout is lost to ipdoctest (pytest#985)."""
  318. if platform.system() != "Darwin":
  319. return
  320. capman = self.config.pluginmanager.getplugin("capturemanager")
  321. if capman:
  322. capman.suspend_global_capture(in_=True)
  323. out, err = capman.read_global_capture()
  324. sys.stdout.write(out)
  325. sys.stderr.write(err)
  326. # TODO: Type ignored -- breaks Liskov Substitution.
  327. def repr_failure( # type: ignore[override]
  328. self,
  329. excinfo: ExceptionInfo[BaseException],
  330. ) -> Union[str, TerminalRepr]:
  331. import doctest
  332. failures: Optional[
  333. Sequence[Union[doctest.DocTestFailure, doctest.UnexpectedException]]
  334. ] = None
  335. if isinstance(
  336. excinfo.value, (doctest.DocTestFailure, doctest.UnexpectedException)
  337. ):
  338. failures = [excinfo.value]
  339. elif isinstance(excinfo.value, MultipleDoctestFailures):
  340. failures = excinfo.value.failures
  341. if failures is None:
  342. return super().repr_failure(excinfo)
  343. reprlocation_lines = []
  344. for failure in failures:
  345. example = failure.example
  346. test = failure.test
  347. filename = test.filename
  348. if test.lineno is None:
  349. lineno = None
  350. else:
  351. lineno = test.lineno + example.lineno + 1
  352. message = type(failure).__name__
  353. # TODO: ReprFileLocation doesn't expect a None lineno.
  354. reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
  355. checker = _get_checker()
  356. report_choice = _get_report_choice(self.config.getoption("ipdoctestreport"))
  357. if lineno is not None:
  358. assert failure.test.docstring is not None
  359. lines = failure.test.docstring.splitlines(False)
  360. # add line numbers to the left of the error message
  361. assert test.lineno is not None
  362. lines = [
  363. "%03d %s" % (i + test.lineno + 1, x) for (i, x) in enumerate(lines)
  364. ]
  365. # trim docstring error lines to 10
  366. lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
  367. else:
  368. lines = [
  369. "EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
  370. ]
  371. indent = ">>>"
  372. for line in example.source.splitlines():
  373. lines.append(f"??? {indent} {line}")
  374. indent = "..."
  375. if isinstance(failure, doctest.DocTestFailure):
  376. lines += checker.output_difference(
  377. example, failure.got, report_choice
  378. ).split("\n")
  379. else:
  380. inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
  381. lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
  382. lines += [
  383. x.strip("\n") for x in traceback.format_exception(*failure.exc_info)
  384. ]
  385. reprlocation_lines.append((reprlocation, lines))
  386. return ReprFailDoctest(reprlocation_lines)
  387. def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
  388. assert self.dtest is not None
  389. return self.path, self.dtest.lineno, "[ipdoctest] %s" % self.name
  390. if int(pytest.__version__.split(".")[0]) < 7:
  391. @property
  392. def path(self) -> Path:
  393. return Path(self.fspath)
  394. def _get_flag_lookup() -> Dict[str, int]:
  395. import doctest
  396. return dict(
  397. DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1,
  398. DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE,
  399. NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE,
  400. ELLIPSIS=doctest.ELLIPSIS,
  401. IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL,
  402. COMPARISON_FLAGS=doctest.COMPARISON_FLAGS,
  403. ALLOW_UNICODE=_get_allow_unicode_flag(),
  404. ALLOW_BYTES=_get_allow_bytes_flag(),
  405. NUMBER=_get_number_flag(),
  406. )
  407. def get_optionflags(parent):
  408. optionflags_str = parent.config.getini("ipdoctest_optionflags")
  409. flag_lookup_table = _get_flag_lookup()
  410. flag_acc = 0
  411. for flag in optionflags_str:
  412. flag_acc |= flag_lookup_table[flag]
  413. return flag_acc
  414. def _get_continue_on_failure(config):
  415. continue_on_failure = config.getvalue("ipdoctest_continue_on_failure")
  416. if continue_on_failure:
  417. # We need to turn off this if we use pdb since we should stop at
  418. # the first failure.
  419. if config.getvalue("usepdb"):
  420. continue_on_failure = False
  421. return continue_on_failure
  422. class IPDoctestTextfile(pytest.Module):
  423. obj = None
  424. def collect(self) -> Iterable[IPDoctestItem]:
  425. import doctest
  426. from .ipdoctest import IPDocTestParser
  427. # Inspired by doctest.testfile; ideally we would use it directly,
  428. # but it doesn't support passing a custom checker.
  429. encoding = self.config.getini("ipdoctest_encoding")
  430. text = self.path.read_text(encoding)
  431. filename = str(self.path)
  432. name = self.path.name
  433. globs = {"__name__": "__main__"}
  434. optionflags = get_optionflags(self)
  435. runner = _get_runner(
  436. verbose=False,
  437. optionflags=optionflags,
  438. checker=_get_checker(),
  439. continue_on_failure=_get_continue_on_failure(self.config),
  440. )
  441. parser = IPDocTestParser()
  442. test = parser.get_doctest(text, globs, name, filename, 0)
  443. if test.examples:
  444. yield IPDoctestItem.from_parent(
  445. self, name=test.name, runner=runner, dtest=test
  446. )
  447. if int(pytest.__version__.split(".")[0]) < 7:
  448. @property
  449. def path(self) -> Path:
  450. return Path(self.fspath)
  451. @classmethod
  452. def from_parent(
  453. cls,
  454. parent,
  455. *,
  456. fspath=None,
  457. path: Optional[Path] = None,
  458. **kw,
  459. ):
  460. if path is not None:
  461. import py.path
  462. fspath = py.path.local(path)
  463. return super().from_parent(parent=parent, fspath=fspath, **kw)
  464. def _check_all_skipped(test: "doctest.DocTest") -> None:
  465. """Raise pytest.skip() if all examples in the given DocTest have the SKIP
  466. option set."""
  467. import doctest
  468. all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
  469. if all_skipped:
  470. pytest.skip("all docstests skipped by +SKIP option")
  471. def _is_mocked(obj: object) -> bool:
  472. """Return if an object is possibly a mock object by checking the
  473. existence of a highly improbable attribute."""
  474. return (
  475. safe_getattr(obj, "pytest_mock_example_attribute_that_shouldnt_exist", None)
  476. is not None
  477. )
  478. @contextmanager
  479. def _patch_unwrap_mock_aware() -> Generator[None, None, None]:
  480. """Context manager which replaces ``inspect.unwrap`` with a version
  481. that's aware of mock objects and doesn't recurse into them."""
  482. real_unwrap = inspect.unwrap
  483. def _mock_aware_unwrap(
  484. func: Callable[..., Any], *, stop: Optional[Callable[[Any], Any]] = None
  485. ) -> Any:
  486. try:
  487. if stop is None or stop is _is_mocked:
  488. return real_unwrap(func, stop=_is_mocked)
  489. _stop = stop
  490. return real_unwrap(func, stop=lambda obj: _is_mocked(obj) or _stop(func))
  491. except Exception as e:
  492. warnings.warn(
  493. "Got %r when unwrapping %r. This is usually caused "
  494. "by a violation of Python's object protocol; see e.g. "
  495. "https://github.com/pytest-dev/pytest/issues/5080" % (e, func),
  496. PytestWarning,
  497. )
  498. raise
  499. inspect.unwrap = _mock_aware_unwrap
  500. try:
  501. yield
  502. finally:
  503. inspect.unwrap = real_unwrap
  504. class IPDoctestModule(pytest.Module):
  505. def collect(self) -> Iterable[IPDoctestItem]:
  506. import doctest
  507. from .ipdoctest import DocTestFinder, IPDocTestParser
  508. class MockAwareDocTestFinder(DocTestFinder):
  509. """A hackish ipdoctest finder that overrides stdlib internals to fix a stdlib bug.
  510. https://github.com/pytest-dev/pytest/issues/3456
  511. https://bugs.python.org/issue25532
  512. """
  513. def _find_lineno(self, obj, source_lines):
  514. """Doctest code does not take into account `@property`, this
  515. is a hackish way to fix it. https://bugs.python.org/issue17446
  516. Wrapped Doctests will need to be unwrapped so the correct
  517. line number is returned. This will be reported upstream. #8796
  518. """
  519. if isinstance(obj, property):
  520. obj = getattr(obj, "fget", obj)
  521. if hasattr(obj, "__wrapped__"):
  522. # Get the main obj in case of it being wrapped
  523. obj = inspect.unwrap(obj)
  524. # Type ignored because this is a private function.
  525. return super()._find_lineno( # type:ignore[misc]
  526. obj,
  527. source_lines,
  528. )
  529. def _find(
  530. self, tests, obj, name, module, source_lines, globs, seen
  531. ) -> None:
  532. if _is_mocked(obj):
  533. return
  534. with _patch_unwrap_mock_aware():
  535. # Type ignored because this is a private function.
  536. super()._find( # type:ignore[misc]
  537. tests, obj, name, module, source_lines, globs, seen
  538. )
  539. if self.path.name == "conftest.py":
  540. if int(pytest.__version__.split(".")[0]) < 7:
  541. module = self.config.pluginmanager._importconftest(
  542. self.path,
  543. self.config.getoption("importmode"),
  544. )
  545. else:
  546. module = self.config.pluginmanager._importconftest(
  547. self.path,
  548. self.config.getoption("importmode"),
  549. rootpath=self.config.rootpath,
  550. )
  551. else:
  552. try:
  553. module = import_path(self.path, root=self.config.rootpath)
  554. except ImportError:
  555. if self.config.getvalue("ipdoctest_ignore_import_errors"):
  556. pytest.skip("unable to import module %r" % self.path)
  557. else:
  558. raise
  559. # Uses internal doctest module parsing mechanism.
  560. finder = MockAwareDocTestFinder(parser=IPDocTestParser())
  561. optionflags = get_optionflags(self)
  562. runner = _get_runner(
  563. verbose=False,
  564. optionflags=optionflags,
  565. checker=_get_checker(),
  566. continue_on_failure=_get_continue_on_failure(self.config),
  567. )
  568. for test in finder.find(module, module.__name__):
  569. if test.examples: # skip empty ipdoctests
  570. yield IPDoctestItem.from_parent(
  571. self, name=test.name, runner=runner, dtest=test
  572. )
  573. if int(pytest.__version__.split(".")[0]) < 7:
  574. @property
  575. def path(self) -> Path:
  576. return Path(self.fspath)
  577. @classmethod
  578. def from_parent(
  579. cls,
  580. parent,
  581. *,
  582. fspath=None,
  583. path: Optional[Path] = None,
  584. **kw,
  585. ):
  586. if path is not None:
  587. import py.path
  588. fspath = py.path.local(path)
  589. return super().from_parent(parent=parent, fspath=fspath, **kw)
  590. def _setup_fixtures(doctest_item: IPDoctestItem) -> FixtureRequest:
  591. """Used by IPDoctestTextfile and IPDoctestItem to setup fixture information."""
  592. def func() -> None:
  593. pass
  594. doctest_item.funcargs = {} # type: ignore[attr-defined]
  595. fm = doctest_item.session._fixturemanager
  596. doctest_item._fixtureinfo = fm.getfixtureinfo( # type: ignore[attr-defined]
  597. node=doctest_item, func=func, cls=None, funcargs=False
  598. )
  599. fixture_request = FixtureRequest(doctest_item, _ispytest=True)
  600. fixture_request._fillfixtures()
  601. return fixture_request
  602. def _init_checker_class() -> Type["IPDoctestOutputChecker"]:
  603. import doctest
  604. import re
  605. from .ipdoctest import IPDoctestOutputChecker
  606. class LiteralsOutputChecker(IPDoctestOutputChecker):
  607. # Based on doctest_nose_plugin.py from the nltk project
  608. # (https://github.com/nltk/nltk) and on the "numtest" doctest extension
  609. # by Sebastien Boisgerault (https://github.com/boisgera/numtest).
  610. _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
  611. _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE)
  612. _number_re = re.compile(
  613. r"""
  614. (?P<number>
  615. (?P<mantissa>
  616. (?P<integer1> [+-]?\d*)\.(?P<fraction>\d+)
  617. |
  618. (?P<integer2> [+-]?\d+)\.
  619. )
  620. (?:
  621. [Ee]
  622. (?P<exponent1> [+-]?\d+)
  623. )?
  624. |
  625. (?P<integer3> [+-]?\d+)
  626. (?:
  627. [Ee]
  628. (?P<exponent2> [+-]?\d+)
  629. )
  630. )
  631. """,
  632. re.VERBOSE,
  633. )
  634. def check_output(self, want: str, got: str, optionflags: int) -> bool:
  635. if super().check_output(want, got, optionflags):
  636. return True
  637. allow_unicode = optionflags & _get_allow_unicode_flag()
  638. allow_bytes = optionflags & _get_allow_bytes_flag()
  639. allow_number = optionflags & _get_number_flag()
  640. if not allow_unicode and not allow_bytes and not allow_number:
  641. return False
  642. def remove_prefixes(regex: Pattern[str], txt: str) -> str:
  643. return re.sub(regex, r"\1\2", txt)
  644. if allow_unicode:
  645. want = remove_prefixes(self._unicode_literal_re, want)
  646. got = remove_prefixes(self._unicode_literal_re, got)
  647. if allow_bytes:
  648. want = remove_prefixes(self._bytes_literal_re, want)
  649. got = remove_prefixes(self._bytes_literal_re, got)
  650. if allow_number:
  651. got = self._remove_unwanted_precision(want, got)
  652. return super().check_output(want, got, optionflags)
  653. def _remove_unwanted_precision(self, want: str, got: str) -> str:
  654. wants = list(self._number_re.finditer(want))
  655. gots = list(self._number_re.finditer(got))
  656. if len(wants) != len(gots):
  657. return got
  658. offset = 0
  659. for w, g in zip(wants, gots):
  660. fraction: Optional[str] = w.group("fraction")
  661. exponent: Optional[str] = w.group("exponent1")
  662. if exponent is None:
  663. exponent = w.group("exponent2")
  664. precision = 0 if fraction is None else len(fraction)
  665. if exponent is not None:
  666. precision -= int(exponent)
  667. if float(w.group()) == approx(float(g.group()), abs=10**-precision):
  668. # They're close enough. Replace the text we actually
  669. # got with the text we want, so that it will match when we
  670. # check the string literally.
  671. got = (
  672. got[: g.start() + offset] + w.group() + got[g.end() + offset :]
  673. )
  674. offset += w.end() - w.start() - (g.end() - g.start())
  675. return got
  676. return LiteralsOutputChecker
  677. def _get_checker() -> "IPDoctestOutputChecker":
  678. """Return a IPDoctestOutputChecker subclass that supports some
  679. additional options:
  680. * ALLOW_UNICODE and ALLOW_BYTES options to ignore u'' and b''
  681. prefixes (respectively) in string literals. Useful when the same
  682. ipdoctest should run in Python 2 and Python 3.
  683. * NUMBER to ignore floating-point differences smaller than the
  684. precision of the literal number in the ipdoctest.
  685. An inner class is used to avoid importing "ipdoctest" at the module
  686. level.
  687. """
  688. global CHECKER_CLASS
  689. if CHECKER_CLASS is None:
  690. CHECKER_CLASS = _init_checker_class()
  691. return CHECKER_CLASS()
  692. def _get_allow_unicode_flag() -> int:
  693. """Register and return the ALLOW_UNICODE flag."""
  694. import doctest
  695. return doctest.register_optionflag("ALLOW_UNICODE")
  696. def _get_allow_bytes_flag() -> int:
  697. """Register and return the ALLOW_BYTES flag."""
  698. import doctest
  699. return doctest.register_optionflag("ALLOW_BYTES")
  700. def _get_number_flag() -> int:
  701. """Register and return the NUMBER flag."""
  702. import doctest
  703. return doctest.register_optionflag("NUMBER")
  704. def _get_report_choice(key: str) -> int:
  705. """Return the actual `ipdoctest` module flag value.
  706. We want to do it as late as possible to avoid importing `ipdoctest` and all
  707. its dependencies when parsing options, as it adds overhead and breaks tests.
  708. """
  709. import doctest
  710. return {
  711. DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF,
  712. DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF,
  713. DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF,
  714. DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE,
  715. DOCTEST_REPORT_CHOICE_NONE: 0,
  716. }[key]
  717. @pytest.fixture(scope="session")
  718. def ipdoctest_namespace() -> Dict[str, Any]:
  719. """Fixture that returns a :py:class:`dict` that will be injected into the
  720. namespace of ipdoctests."""
  721. return dict()