pytest_ipdoctest.py 29 KB

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