Merge pull request #6601 from blueyed/typing-pytest_collection

typing: pytest_collection
This commit is contained in:
Daniel Hahler 2020-01-30 20:47:06 +01:00 committed by GitHub
commit e830432592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 6 deletions

View File

@ -7,6 +7,10 @@ from typing import Optional
from _pytest.assertion import rewrite
from _pytest.assertion import truncate
from _pytest.assertion import util
from _pytest.compat import TYPE_CHECKING
if TYPE_CHECKING:
from _pytest.main import Session
def pytest_addoption(parser):
@ -91,7 +95,7 @@ def install_importhook(config):
return hook
def pytest_collection(session):
def pytest_collection(session: "Session") -> None:
# this hook is only called when test modules are collected
# so for example not in the master process of pytest-xdist
# (which does not collect test modules)

View File

@ -1,6 +1,14 @@
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
from typing import Any
from typing import Optional
from pluggy import HookspecMarker
from _pytest.compat import TYPE_CHECKING
if TYPE_CHECKING:
from _pytest.main import Session
hookspec = HookspecMarker("pytest")
@ -158,7 +166,7 @@ def pytest_load_initial_conftests(early_config, parser, args):
@hookspec(firstresult=True)
def pytest_collection(session):
def pytest_collection(session: "Session") -> Optional[Any]:
"""Perform the collection protocol for the given session.
Stops at first non-None result, see :ref:`firstresult`.

View File

@ -5,6 +5,7 @@ from contextlib import contextmanager
from io import StringIO
from typing import AbstractSet
from typing import Dict
from typing import Generator
from typing import List
from typing import Mapping
@ -591,7 +592,7 @@ class LoggingPlugin:
) is not None or self._config.getini("log_cli")
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection(self):
def pytest_collection(self) -> Generator[None, None, None]:
with self.live_logs_context():
if self.log_cli_handler:
self.log_cli_handler.set_when("collection")

View File

@ -8,6 +8,8 @@ import sys
from typing import Dict
from typing import FrozenSet
from typing import List
from typing import Optional
from typing import Union
import attr
import py
@ -249,7 +251,7 @@ def pytest_cmdline_main(config):
return wrap_session(config, _main)
def _main(config, session):
def _main(config: Config, session: "Session") -> Optional[Union[int, ExitCode]]:
""" default command line protocol for initialization, session,
running tests and reporting. """
config.hook.pytest_collection(session=session)
@ -259,6 +261,7 @@ def _main(config, session):
return ExitCode.TESTS_FAILED
elif session.testscollected == 0:
return ExitCode.NO_TESTS_COLLECTED
return None
def pytest_collection(session):

View File

@ -514,7 +514,7 @@ class TerminalReporter:
# py < 1.6.0
return self._tw.chars_on_current_line
def pytest_collection(self):
def pytest_collection(self) -> None:
if self.isatty:
if self.config.option.verbose >= 0:
self.write("collecting ... ", bold=True)

View File

@ -1,8 +1,10 @@
import sys
import warnings
from contextlib import contextmanager
from typing import Generator
import pytest
from _pytest.main import Session
def _setoption(wmod, arg):
@ -117,7 +119,7 @@ def pytest_runtest_protocol(item):
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection(session):
def pytest_collection(session: Session) -> Generator[None, None, None]:
config = session.config
with catch_warnings_for_item(
config=config, ihook=config.hook, when="collect", item=None