Skip to content

Commit 8e7ce60

Browse files
authored
Export pytest.DoctestItem for typing / runtime purposes (#10313)
Fixes #10312
1 parent 2bc036e commit 8e7ce60

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

changelog/10313.trivial.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Made ``_pytest.doctest.DoctestItem`` export ``pytest.DoctestItem`` for
2+
type check and runtime purposes. Made `_pytest.doctest` use internal APIs
3+
to avoid circular imports.

src/_pytest/doctest.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from typing import TYPE_CHECKING
2424
from typing import Union
2525

26-
import pytest
2726
from _pytest import outcomes
2827
from _pytest._code.code import ExceptionInfo
2928
from _pytest._code.code import ReprFileLocation
@@ -32,11 +31,15 @@
3231
from _pytest.compat import safe_getattr
3332
from _pytest.config import Config
3433
from _pytest.config.argparsing import Parser
34+
from _pytest.fixtures import fixture
3535
from _pytest.fixtures import FixtureRequest
3636
from _pytest.nodes import Collector
37+
from _pytest.nodes import Item
3738
from _pytest.outcomes import OutcomeException
39+
from _pytest.outcomes import skip
3840
from _pytest.pathlib import fnmatch_ex
3941
from _pytest.pathlib import import_path
42+
from _pytest.python import Module
4043
from _pytest.python_api import approx
4144
from _pytest.warning_types import PytestWarning
4245

@@ -246,7 +249,7 @@ def _get_runner(
246249
)
247250

248251

249-
class DoctestItem(pytest.Item):
252+
class DoctestItem(Item):
250253
def __init__(
251254
self,
252255
name: str,
@@ -411,7 +414,7 @@ def _get_continue_on_failure(config):
411414
return continue_on_failure
412415

413416

414-
class DoctestTextfile(pytest.Module):
417+
class DoctestTextfile(Module):
415418
obj = None
416419

417420
def collect(self) -> Iterable[DoctestItem]:
@@ -449,7 +452,7 @@ def _check_all_skipped(test: "doctest.DocTest") -> None:
449452

450453
all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
451454
if all_skipped:
452-
pytest.skip("all tests skipped by +SKIP option")
455+
skip("all tests skipped by +SKIP option")
453456

454457

455458
def _is_mocked(obj: object) -> bool:
@@ -491,7 +494,7 @@ def _mock_aware_unwrap(
491494
inspect.unwrap = real_unwrap
492495

493496

494-
class DoctestModule(pytest.Module):
497+
class DoctestModule(Module):
495498
def collect(self) -> Iterable[DoctestItem]:
496499
import doctest
497500

@@ -549,7 +552,7 @@ def _find(
549552
)
550553
except ImportError:
551554
if self.config.getvalue("doctest_ignore_import_errors"):
552-
pytest.skip("unable to import module %r" % self.path)
555+
skip("unable to import module %r" % self.path)
553556
else:
554557
raise
555558
# Uses internal doctest module parsing mechanism.
@@ -731,7 +734,7 @@ def _get_report_choice(key: str) -> int:
731734
}[key]
732735

733736

734-
@pytest.fixture(scope="session")
737+
@fixture(scope="session")
735738
def doctest_namespace() -> Dict[str, Any]:
736739
"""Fixture that returns a :py:class:`dict` that will be injected into the
737740
namespace of doctests.

src/pytest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from _pytest.config.argparsing import OptionGroup
1919
from _pytest.config.argparsing import Parser
2020
from _pytest.debugging import pytestPDB as __pytestPDB
21+
from _pytest.doctest import DoctestItem
2122
from _pytest.fixtures import fixture
2223
from _pytest.fixtures import FixtureLookupError
2324
from _pytest.fixtures import FixtureRequest
@@ -92,6 +93,7 @@
9293
"Config",
9394
"console_main",
9495
"deprecated_call",
96+
"DoctestItem",
9597
"exit",
9698
"ExceptionInfo",
9799
"ExitCode",

0 commit comments

Comments
 (0)