pytest_ipdoctest.py 29 KB

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