pytest_ipdoctest.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 builtins
  8. import bdb
  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 Any
  19. from typing import Callable
  20. from typing import Dict
  21. from typing import Generator
  22. from typing import Iterable
  23. from typing import List
  24. from typing import Optional
  25. from typing import Pattern
  26. from typing import Sequence
  27. from typing import Tuple
  28. from typing import Type
  29. from typing import TYPE_CHECKING
  30. from typing import Union
  31. import pytest
  32. from _pytest import outcomes
  33. from _pytest._code.code import ExceptionInfo
  34. from _pytest._code.code import ReprFileLocation
  35. from _pytest._code.code import 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
  44. from _pytest.pathlib import import_path
  45. from _pytest.python_api import approx
  46. from _pytest.warning_types import PytestWarning
  47. if TYPE_CHECKING:
  48. import doctest
  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. def __init__(
  234. self,
  235. name: str,
  236. parent: "Union[IPDoctestTextfile, IPDoctestModule]",
  237. runner: Optional["IPDocTestRunner"] = None,
  238. dtest: Optional["doctest.DocTest"] = None,
  239. ) -> None:
  240. super().__init__(name, parent)
  241. self.runner = runner
  242. self.dtest = dtest
  243. self.obj = None
  244. self.fixture_request: Optional[FixtureRequest] = None
  245. @classmethod
  246. def from_parent( # type: ignore
  247. cls,
  248. parent: "Union[IPDoctestTextfile, IPDoctestModule]",
  249. *,
  250. name: str,
  251. runner: "IPDocTestRunner",
  252. dtest: "doctest.DocTest",
  253. ):
  254. # incompatible signature due to imposed limits on subclass
  255. """The public named constructor."""
  256. return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)
  257. def setup(self) -> None:
  258. if self.dtest is not None:
  259. self.fixture_request = _setup_fixtures(self)
  260. globs = dict(getfixture=self.fixture_request.getfixturevalue)
  261. for name, value in self.fixture_request.getfixturevalue(
  262. "ipdoctest_namespace"
  263. ).items():
  264. globs[name] = value
  265. self.dtest.globs.update(globs)
  266. from .ipdoctest import IPExample
  267. if isinstance(self.dtest.examples[0], IPExample):
  268. # for IPython examples *only*, we swap the globals with the ipython
  269. # namespace, after updating it with the globals (which doctest
  270. # fills with the necessary info from the module being tested).
  271. self._user_ns_orig = {}
  272. self._user_ns_orig.update(_ip.user_ns)
  273. _ip.user_ns.update(self.dtest.globs)
  274. # We must remove the _ key in the namespace, so that Python's
  275. # doctest code sets it naturally
  276. _ip.user_ns.pop("_", None)
  277. _ip.user_ns["__builtins__"] = builtins
  278. self.dtest.globs = _ip.user_ns
  279. def teardown(self) -> None:
  280. from .ipdoctest import IPExample
  281. # Undo the test.globs reassignment we made
  282. if isinstance(self.dtest.examples[0], IPExample):
  283. self.dtest.globs = {}
  284. _ip.user_ns.clear()
  285. _ip.user_ns.update(self._user_ns_orig)
  286. del self._user_ns_orig
  287. self.dtest.globs.clear()
  288. def runtest(self) -> None:
  289. assert self.dtest is not None
  290. assert self.runner is not None
  291. _check_all_skipped(self.dtest)
  292. self._disable_output_capturing_for_darwin()
  293. failures: List["doctest.DocTestFailure"] = []
  294. # exec(compile(..., "single", ...), ...) puts result in builtins._
  295. had_underscore_value = hasattr(builtins, "_")
  296. underscore_original_value = getattr(builtins, "_", None)
  297. # Save our current directory and switch out to the one where the
  298. # test was originally created, in case another doctest did a
  299. # directory change. We'll restore this in the finally clause.
  300. curdir = os.getcwd()
  301. os.chdir(self.fspath.dirname)
  302. try:
  303. # Type ignored because we change the type of `out` from what
  304. # ipdoctest expects.
  305. self.runner.run(self.dtest, out=failures, clear_globs=False) # type: ignore[arg-type]
  306. finally:
  307. os.chdir(curdir)
  308. if had_underscore_value:
  309. setattr(builtins, "_", underscore_original_value)
  310. elif hasattr(builtins, "_"):
  311. delattr(builtins, "_")
  312. if failures:
  313. raise MultipleDoctestFailures(failures)
  314. def _disable_output_capturing_for_darwin(self) -> None:
  315. """Disable output capturing. Otherwise, stdout is lost to ipdoctest (pytest#985)."""
  316. if platform.system() != "Darwin":
  317. return
  318. capman = self.config.pluginmanager.getplugin("capturemanager")
  319. if capman:
  320. capman.suspend_global_capture(in_=True)
  321. out, err = capman.read_global_capture()
  322. sys.stdout.write(out)
  323. sys.stderr.write(err)
  324. # TODO: Type ignored -- breaks Liskov Substitution.
  325. def repr_failure( # type: ignore[override]
  326. self,
  327. excinfo: ExceptionInfo[BaseException],
  328. ) -> Union[str, TerminalRepr]:
  329. import doctest
  330. failures: Optional[
  331. Sequence[Union[doctest.DocTestFailure, doctest.UnexpectedException]]
  332. ] = None
  333. if isinstance(
  334. excinfo.value, (doctest.DocTestFailure, doctest.UnexpectedException)
  335. ):
  336. failures = [excinfo.value]
  337. elif isinstance(excinfo.value, MultipleDoctestFailures):
  338. failures = excinfo.value.failures
  339. if failures is None:
  340. return super().repr_failure(excinfo)
  341. reprlocation_lines = []
  342. for failure in failures:
  343. example = failure.example
  344. test = failure.test
  345. filename = test.filename
  346. if test.lineno is None:
  347. lineno = None
  348. else:
  349. lineno = test.lineno + example.lineno + 1
  350. message = type(failure).__name__
  351. # TODO: ReprFileLocation doesn't expect a None lineno.
  352. reprlocation = ReprFileLocation(filename, lineno, message) # type: ignore[arg-type]
  353. checker = _get_checker()
  354. report_choice = _get_report_choice(self.config.getoption("ipdoctestreport"))
  355. if lineno is not None:
  356. assert failure.test.docstring is not None
  357. lines = failure.test.docstring.splitlines(False)
  358. # add line numbers to the left of the error message
  359. assert test.lineno is not None
  360. lines = [
  361. "%03d %s" % (i + test.lineno + 1, x) for (i, x) in enumerate(lines)
  362. ]
  363. # trim docstring error lines to 10
  364. lines = lines[max(example.lineno - 9, 0) : example.lineno + 1]
  365. else:
  366. lines = [
  367. "EXAMPLE LOCATION UNKNOWN, not showing all tests of that example"
  368. ]
  369. indent = ">>>"
  370. for line in example.source.splitlines():
  371. lines.append(f"??? {indent} {line}")
  372. indent = "..."
  373. if isinstance(failure, doctest.DocTestFailure):
  374. lines += checker.output_difference(
  375. example, failure.got, report_choice
  376. ).split("\n")
  377. else:
  378. inner_excinfo = ExceptionInfo.from_exc_info(failure.exc_info)
  379. lines += ["UNEXPECTED EXCEPTION: %s" % repr(inner_excinfo.value)]
  380. lines += [
  381. x.strip("\n") for x in traceback.format_exception(*failure.exc_info)
  382. ]
  383. reprlocation_lines.append((reprlocation, lines))
  384. return ReprFailDoctest(reprlocation_lines)
  385. def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
  386. assert self.dtest is not None
  387. return self.path, self.dtest.lineno, "[ipdoctest] %s" % self.name
  388. if int(pytest.__version__.split(".")[0]) < 7:
  389. @property
  390. def path(self) -> Path:
  391. return Path(self.fspath)
  392. def _get_flag_lookup() -> Dict[str, int]:
  393. import doctest
  394. return dict(
  395. DONT_ACCEPT_TRUE_FOR_1=doctest.DONT_ACCEPT_TRUE_FOR_1,
  396. DONT_ACCEPT_BLANKLINE=doctest.DONT_ACCEPT_BLANKLINE,
  397. NORMALIZE_WHITESPACE=doctest.NORMALIZE_WHITESPACE,
  398. ELLIPSIS=doctest.ELLIPSIS,
  399. IGNORE_EXCEPTION_DETAIL=doctest.IGNORE_EXCEPTION_DETAIL,
  400. COMPARISON_FLAGS=doctest.COMPARISON_FLAGS,
  401. ALLOW_UNICODE=_get_allow_unicode_flag(),
  402. ALLOW_BYTES=_get_allow_bytes_flag(),
  403. NUMBER=_get_number_flag(),
  404. )
  405. def get_optionflags(parent):
  406. optionflags_str = parent.config.getini("ipdoctest_optionflags")
  407. flag_lookup_table = _get_flag_lookup()
  408. flag_acc = 0
  409. for flag in optionflags_str:
  410. flag_acc |= flag_lookup_table[flag]
  411. return flag_acc
  412. def _get_continue_on_failure(config):
  413. continue_on_failure = config.getvalue("ipdoctest_continue_on_failure")
  414. if continue_on_failure:
  415. # We need to turn off this if we use pdb since we should stop at
  416. # the first failure.
  417. if config.getvalue("usepdb"):
  418. continue_on_failure = False
  419. return continue_on_failure
  420. class IPDoctestTextfile(pytest.Module):
  421. obj = None
  422. def collect(self) -> Iterable[IPDoctestItem]:
  423. import doctest
  424. from .ipdoctest import IPDocTestParser
  425. # Inspired by doctest.testfile; ideally we would use it directly,
  426. # but it doesn't support passing a custom checker.
  427. encoding = self.config.getini("ipdoctest_encoding")
  428. text = self.path.read_text(encoding)
  429. filename = str(self.path)
  430. name = self.path.name
  431. globs = {"__name__": "__main__"}
  432. optionflags = get_optionflags(self)
  433. runner = _get_runner(
  434. verbose=False,
  435. optionflags=optionflags,
  436. checker=_get_checker(),
  437. continue_on_failure=_get_continue_on_failure(self.config),
  438. )
  439. parser = IPDocTestParser()
  440. test = parser.get_doctest(text, globs, name, filename, 0)
  441. if test.examples:
  442. yield IPDoctestItem.from_parent(
  443. self, name=test.name, runner=runner, dtest=test
  444. )
  445. if int(pytest.__version__.split(".")[0]) < 7:
  446. @property
  447. def path(self) -> Path:
  448. return Path(self.fspath)
  449. @classmethod
  450. def from_parent(
  451. cls,
  452. parent,
  453. *,
  454. fspath=None,
  455. path: Optional[Path] = None,
  456. **kw,
  457. ):
  458. if path is not None:
  459. import py.path
  460. fspath = py.path.local(path)
  461. return super().from_parent(parent=parent, fspath=fspath, **kw)
  462. def _check_all_skipped(test: "doctest.DocTest") -> None:
  463. """Raise pytest.skip() if all examples in the given DocTest have the SKIP
  464. option set."""
  465. import doctest
  466. all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
  467. if all_skipped:
  468. pytest.skip("all docstests skipped by +SKIP option")
  469. def _is_mocked(obj: object) -> bool:
  470. """Return if an object is possibly a mock object by checking the
  471. existence of a highly improbable attribute."""
  472. return (
  473. safe_getattr(obj, "pytest_mock_example_attribute_that_shouldnt_exist", None)
  474. is not None
  475. )
  476. @contextmanager
  477. def _patch_unwrap_mock_aware() -> Generator[None, None, None]:
  478. """Context manager which replaces ``inspect.unwrap`` with a version
  479. that's aware of mock objects and doesn't recurse into them."""
  480. real_unwrap = inspect.unwrap
  481. def _mock_aware_unwrap(
  482. func: Callable[..., Any], *, stop: Optional[Callable[[Any], Any]] = None
  483. ) -> Any:
  484. try:
  485. if stop is None or stop is _is_mocked:
  486. return real_unwrap(func, stop=_is_mocked)
  487. _stop = stop
  488. return real_unwrap(func, stop=lambda obj: _is_mocked(obj) or _stop(func))
  489. except Exception as e:
  490. warnings.warn(
  491. "Got %r when unwrapping %r. This is usually caused "
  492. "by a violation of Python's object protocol; see e.g. "
  493. "https://github.com/pytest-dev/pytest/issues/5080" % (e, func),
  494. PytestWarning,
  495. )
  496. raise
  497. inspect.unwrap = _mock_aware_unwrap
  498. try:
  499. yield
  500. finally:
  501. inspect.unwrap = real_unwrap
  502. class IPDoctestModule(pytest.Module):
  503. def collect(self) -> Iterable[IPDoctestItem]:
  504. import doctest
  505. from .ipdoctest import DocTestFinder, IPDocTestParser
  506. class MockAwareDocTestFinder(DocTestFinder):
  507. """A hackish ipdoctest finder that overrides stdlib internals to fix a stdlib bug.
  508. https://github.com/pytest-dev/pytest/issues/3456
  509. https://bugs.python.org/issue25532
  510. """
  511. def _find_lineno(self, obj, source_lines):
  512. """Doctest code does not take into account `@property`, this
  513. is a hackish way to fix it. https://bugs.python.org/issue17446
  514. Wrapped Doctests will need to be unwrapped so the correct
  515. line number is returned. This will be reported upstream. #8796
  516. """
  517. if isinstance(obj, property):
  518. obj = getattr(obj, "fget", obj)
  519. if hasattr(obj, "__wrapped__"):
  520. # Get the main obj in case of it being wrapped
  521. obj = inspect.unwrap(obj)
  522. # Type ignored because this is a private function.
  523. return super()._find_lineno( # type:ignore[misc]
  524. obj,
  525. source_lines,
  526. )
  527. def _find(
  528. self, tests, obj, name, module, source_lines, globs, seen
  529. ) -> None:
  530. if _is_mocked(obj):
  531. return
  532. with _patch_unwrap_mock_aware():
  533. # Type ignored because this is a private function.
  534. super()._find( # type:ignore[misc]
  535. tests, obj, name, module, source_lines, globs, seen
  536. )
  537. if self.path.name == "conftest.py":
  538. if int(pytest.__version__.split(".")[0]) < 7:
  539. module = self.config.pluginmanager._importconftest(
  540. self.path,
  541. self.config.getoption("importmode"),
  542. )
  543. else:
  544. module = self.config.pluginmanager._importconftest(
  545. self.path,
  546. self.config.getoption("importmode"),
  547. rootpath=self.config.rootpath,
  548. )
  549. else:
  550. try:
  551. module = import_path(self.path, root=self.config.rootpath)
  552. except ImportError:
  553. if self.config.getvalue("ipdoctest_ignore_import_errors"):
  554. pytest.skip("unable to import module %r" % self.path)
  555. else:
  556. raise
  557. # Uses internal doctest module parsing mechanism.
  558. finder = MockAwareDocTestFinder(parser=IPDocTestParser())
  559. optionflags = get_optionflags(self)
  560. runner = _get_runner(
  561. verbose=False,
  562. optionflags=optionflags,
  563. checker=_get_checker(),
  564. continue_on_failure=_get_continue_on_failure(self.config),
  565. )
  566. for test in finder.find(module, module.__name__):
  567. if test.examples: # skip empty ipdoctests
  568. yield IPDoctestItem.from_parent(
  569. self, name=test.name, runner=runner, dtest=test
  570. )
  571. if int(pytest.__version__.split(".")[0]) < 7:
  572. @property
  573. def path(self) -> Path:
  574. return Path(self.fspath)
  575. @classmethod
  576. def from_parent(
  577. cls,
  578. parent,
  579. *,
  580. fspath=None,
  581. path: Optional[Path] = None,
  582. **kw,
  583. ):
  584. if path is not None:
  585. import py.path
  586. fspath = py.path.local(path)
  587. return super().from_parent(parent=parent, fspath=fspath, **kw)
  588. def _setup_fixtures(doctest_item: IPDoctestItem) -> FixtureRequest:
  589. """Used by IPDoctestTextfile and IPDoctestItem to setup fixture information."""
  590. def func() -> None:
  591. pass
  592. doctest_item.funcargs = {} # type: ignore[attr-defined]
  593. fm = doctest_item.session._fixturemanager
  594. doctest_item._fixtureinfo = fm.getfixtureinfo( # type: ignore[attr-defined]
  595. node=doctest_item, func=func, cls=None, funcargs=False
  596. )
  597. fixture_request = FixtureRequest(doctest_item, _ispytest=True)
  598. fixture_request._fillfixtures()
  599. return fixture_request
  600. def _init_checker_class() -> Type["IPDoctestOutputChecker"]:
  601. import doctest
  602. import re
  603. from .ipdoctest import IPDoctestOutputChecker
  604. class LiteralsOutputChecker(IPDoctestOutputChecker):
  605. # Based on doctest_nose_plugin.py from the nltk project
  606. # (https://github.com/nltk/nltk) and on the "numtest" doctest extension
  607. # by Sebastien Boisgerault (https://github.com/boisgera/numtest).
  608. _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
  609. _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE)
  610. _number_re = re.compile(
  611. r"""
  612. (?P<number>
  613. (?P<mantissa>
  614. (?P<integer1> [+-]?\d*)\.(?P<fraction>\d+)
  615. |
  616. (?P<integer2> [+-]?\d+)\.
  617. )
  618. (?:
  619. [Ee]
  620. (?P<exponent1> [+-]?\d+)
  621. )?
  622. |
  623. (?P<integer3> [+-]?\d+)
  624. (?:
  625. [Ee]
  626. (?P<exponent2> [+-]?\d+)
  627. )
  628. )
  629. """,
  630. re.VERBOSE,
  631. )
  632. def check_output(self, want: str, got: str, optionflags: int) -> bool:
  633. if super().check_output(want, got, optionflags):
  634. return True
  635. allow_unicode = optionflags & _get_allow_unicode_flag()
  636. allow_bytes = optionflags & _get_allow_bytes_flag()
  637. allow_number = optionflags & _get_number_flag()
  638. if not allow_unicode and not allow_bytes and not allow_number:
  639. return False
  640. def remove_prefixes(regex: Pattern[str], txt: str) -> str:
  641. return re.sub(regex, r"\1\2", txt)
  642. if allow_unicode:
  643. want = remove_prefixes(self._unicode_literal_re, want)
  644. got = remove_prefixes(self._unicode_literal_re, got)
  645. if allow_bytes:
  646. want = remove_prefixes(self._bytes_literal_re, want)
  647. got = remove_prefixes(self._bytes_literal_re, got)
  648. if allow_number:
  649. got = self._remove_unwanted_precision(want, got)
  650. return super().check_output(want, got, optionflags)
  651. def _remove_unwanted_precision(self, want: str, got: str) -> str:
  652. wants = list(self._number_re.finditer(want))
  653. gots = list(self._number_re.finditer(got))
  654. if len(wants) != len(gots):
  655. return got
  656. offset = 0
  657. for w, g in zip(wants, gots):
  658. fraction: Optional[str] = w.group("fraction")
  659. exponent: Optional[str] = w.group("exponent1")
  660. if exponent is None:
  661. exponent = w.group("exponent2")
  662. precision = 0 if fraction is None else len(fraction)
  663. if exponent is not None:
  664. precision -= int(exponent)
  665. if float(w.group()) == approx(float(g.group()), abs=10**-precision):
  666. # They're close enough. Replace the text we actually
  667. # got with the text we want, so that it will match when we
  668. # check the string literally.
  669. got = (
  670. got[: g.start() + offset] + w.group() + got[g.end() + offset :]
  671. )
  672. offset += w.end() - w.start() - (g.end() - g.start())
  673. return got
  674. return LiteralsOutputChecker
  675. def _get_checker() -> "IPDoctestOutputChecker":
  676. """Return a IPDoctestOutputChecker subclass that supports some
  677. additional options:
  678. * ALLOW_UNICODE and ALLOW_BYTES options to ignore u'' and b''
  679. prefixes (respectively) in string literals. Useful when the same
  680. ipdoctest should run in Python 2 and Python 3.
  681. * NUMBER to ignore floating-point differences smaller than the
  682. precision of the literal number in the ipdoctest.
  683. An inner class is used to avoid importing "ipdoctest" at the module
  684. level.
  685. """
  686. global CHECKER_CLASS
  687. if CHECKER_CLASS is None:
  688. CHECKER_CLASS = _init_checker_class()
  689. return CHECKER_CLASS()
  690. def _get_allow_unicode_flag() -> int:
  691. """Register and return the ALLOW_UNICODE flag."""
  692. import doctest
  693. return doctest.register_optionflag("ALLOW_UNICODE")
  694. def _get_allow_bytes_flag() -> int:
  695. """Register and return the ALLOW_BYTES flag."""
  696. import doctest
  697. return doctest.register_optionflag("ALLOW_BYTES")
  698. def _get_number_flag() -> int:
  699. """Register and return the NUMBER flag."""
  700. import doctest
  701. return doctest.register_optionflag("NUMBER")
  702. def _get_report_choice(key: str) -> int:
  703. """Return the actual `ipdoctest` module flag value.
  704. We want to do it as late as possible to avoid importing `ipdoctest` and all
  705. its dependencies when parsing options, as it adds overhead and breaks tests.
  706. """
  707. import doctest
  708. return {
  709. DOCTEST_REPORT_CHOICE_UDIFF: doctest.REPORT_UDIFF,
  710. DOCTEST_REPORT_CHOICE_CDIFF: doctest.REPORT_CDIFF,
  711. DOCTEST_REPORT_CHOICE_NDIFF: doctest.REPORT_NDIFF,
  712. DOCTEST_REPORT_CHOICE_ONLY_FIRST_FAILURE: doctest.REPORT_ONLY_FIRST_FAILURE,
  713. DOCTEST_REPORT_CHOICE_NONE: 0,
  714. }[key]
  715. @pytest.fixture(scope="session")
  716. def ipdoctest_namespace() -> Dict[str, Any]:
  717. """Fixture that returns a :py:class:`dict` that will be injected into the
  718. namespace of ipdoctests."""
  719. return dict()