Export `pytest.DoctestItem` for typing / runtime purposes (#10313)

Fixes #10312
This commit is contained in:
Tony Narlock 2022-10-07 11:03:42 -05:00 committed by GitHub
parent 2bc036e50a
commit 8e7ce60c7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 7 deletions

View File

@ -0,0 +1,3 @@
Made ``_pytest.doctest.DoctestItem`` export ``pytest.DoctestItem`` for
type check and runtime purposes. Made `_pytest.doctest` use internal APIs
to avoid circular imports.

View File

@ -23,7 +23,6 @@ from typing import Type
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Union from typing import Union
import pytest
from _pytest import outcomes from _pytest import outcomes
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation from _pytest._code.code import ReprFileLocation
@ -32,11 +31,15 @@ from _pytest._io import TerminalWriter
from _pytest.compat import safe_getattr from _pytest.compat import safe_getattr
from _pytest.config import Config from _pytest.config import Config
from _pytest.config.argparsing import Parser from _pytest.config.argparsing import Parser
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Collector from _pytest.nodes import Collector
from _pytest.nodes import Item
from _pytest.outcomes import OutcomeException from _pytest.outcomes import OutcomeException
from _pytest.outcomes import skip
from _pytest.pathlib import fnmatch_ex from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import import_path from _pytest.pathlib import import_path
from _pytest.python import Module
from _pytest.python_api import approx from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning from _pytest.warning_types import PytestWarning
@ -246,7 +249,7 @@ def _get_runner(
) )
class DoctestItem(pytest.Item): class DoctestItem(Item):
def __init__( def __init__(
self, self,
name: str, name: str,
@ -411,7 +414,7 @@ def _get_continue_on_failure(config):
return continue_on_failure return continue_on_failure
class DoctestTextfile(pytest.Module): class DoctestTextfile(Module):
obj = None obj = None
def collect(self) -> Iterable[DoctestItem]: def collect(self) -> Iterable[DoctestItem]:
@ -449,7 +452,7 @@ def _check_all_skipped(test: "doctest.DocTest") -> None:
all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples) all_skipped = all(x.options.get(doctest.SKIP, False) for x in test.examples)
if all_skipped: if all_skipped:
pytest.skip("all tests skipped by +SKIP option") skip("all tests skipped by +SKIP option")
def _is_mocked(obj: object) -> bool: def _is_mocked(obj: object) -> bool:
@ -491,7 +494,7 @@ def _patch_unwrap_mock_aware() -> Generator[None, None, None]:
inspect.unwrap = real_unwrap inspect.unwrap = real_unwrap
class DoctestModule(pytest.Module): class DoctestModule(Module):
def collect(self) -> Iterable[DoctestItem]: def collect(self) -> Iterable[DoctestItem]:
import doctest import doctest
@ -549,7 +552,7 @@ class DoctestModule(pytest.Module):
) )
except ImportError: except ImportError:
if self.config.getvalue("doctest_ignore_import_errors"): if self.config.getvalue("doctest_ignore_import_errors"):
pytest.skip("unable to import module %r" % self.path) skip("unable to import module %r" % self.path)
else: else:
raise raise
# Uses internal doctest module parsing mechanism. # Uses internal doctest module parsing mechanism.
@ -731,7 +734,7 @@ def _get_report_choice(key: str) -> int:
}[key] }[key]
@pytest.fixture(scope="session") @fixture(scope="session")
def doctest_namespace() -> Dict[str, Any]: def doctest_namespace() -> Dict[str, Any]:
"""Fixture that returns a :py:class:`dict` that will be injected into the """Fixture that returns a :py:class:`dict` that will be injected into the
namespace of doctests. namespace of doctests.

View File

@ -18,6 +18,7 @@ from _pytest.config import UsageError
from _pytest.config.argparsing import OptionGroup from _pytest.config.argparsing import OptionGroup
from _pytest.config.argparsing import Parser from _pytest.config.argparsing import Parser
from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.debugging import pytestPDB as __pytestPDB
from _pytest.doctest import DoctestItem
from _pytest.fixtures import fixture from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureLookupError
from _pytest.fixtures import FixtureRequest from _pytest.fixtures import FixtureRequest
@ -92,6 +93,7 @@ __all__ = [
"Config", "Config",
"console_main", "console_main",
"deprecated_call", "deprecated_call",
"DoctestItem",
"exit", "exit",
"ExceptionInfo", "ExceptionInfo",
"ExitCode", "ExitCode",