Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export pytest.DoctestItem for typing / runtime purposes #10313

Merged
merged 6 commits into from Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/10313.trivial.rst
@@ -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.
17 changes: 10 additions & 7 deletions src/_pytest/doctest.py
Expand Up @@ -23,7 +23,6 @@
from typing import TYPE_CHECKING
from typing import Union

import pytest
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
Expand All @@ -32,11 +31,15 @@
from _pytest.compat import safe_getattr
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureRequest
from _pytest.nodes import Collector
from _pytest.nodes import Item
from _pytest.outcomes import OutcomeException
from _pytest.outcomes import skip
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import import_path
from _pytest.python import Module
from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning

Expand Down Expand Up @@ -246,7 +249,7 @@ def _get_runner(
)


class DoctestItem(pytest.Item):
class DoctestItem(Item):
def __init__(
self,
name: str,
Expand Down Expand Up @@ -411,7 +414,7 @@ def _get_continue_on_failure(config):
return continue_on_failure


class DoctestTextfile(pytest.Module):
class DoctestTextfile(Module):
obj = None

def collect(self) -> Iterable[DoctestItem]:
Expand Down Expand Up @@ -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)
if all_skipped:
pytest.skip("all tests skipped by +SKIP option")
skip("all tests skipped by +SKIP option")


def _is_mocked(obj: object) -> bool:
Expand Down Expand Up @@ -491,7 +494,7 @@ def _mock_aware_unwrap(
inspect.unwrap = real_unwrap


class DoctestModule(pytest.Module):
class DoctestModule(Module):
def collect(self) -> Iterable[DoctestItem]:
import doctest

Expand Down Expand Up @@ -549,7 +552,7 @@ def _find(
)
except ImportError:
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:
raise
# Uses internal doctest module parsing mechanism.
Expand Down Expand Up @@ -731,7 +734,7 @@ def _get_report_choice(key: str) -> int:
}[key]


@pytest.fixture(scope="session")
@fixture(scope="session")
def doctest_namespace() -> Dict[str, Any]:
"""Fixture that returns a :py:class:`dict` that will be injected into the
namespace of doctests.
Expand Down
2 changes: 2 additions & 0 deletions src/pytest/__init__.py
Expand Up @@ -18,6 +18,7 @@
from _pytest.config.argparsing import OptionGroup
from _pytest.config.argparsing import Parser
from _pytest.debugging import pytestPDB as __pytestPDB
from _pytest.doctest import DoctestItem
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureLookupError
from _pytest.fixtures import FixtureRequest
Expand Down Expand Up @@ -92,6 +93,7 @@
"Config",
"console_main",
"deprecated_call",
"DoctestItem",
"exit",
"ExceptionInfo",
"ExitCode",
Expand Down