Skip to content

typing: pytest_collection #6601

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

Merged
merged 1 commit into from
Jan 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/_pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
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):
Expand Down Expand Up @@ -91,7 +95,7 @@ def undo():
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)
Expand Down
10 changes: 9 additions & 1 deletion src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional[Any]: the return values are not used, this is meant to indicate that.

"""Perform the collection protocol for the given session.

Stops at first non-None result, see :ref:`firstresult`.
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from io import StringIO
from typing import AbstractSet
from typing import Dict
from typing import Generator
from typing import List
from typing import Mapping

Expand Down Expand Up @@ -591,7 +592,7 @@ def _log_cli_enabled(self):
) 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")
Expand Down
5 changes: 4 additions & 1 deletion src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from typing import Dict
from typing import FrozenSet
from typing import List
from typing import Optional
from typing import Union

import attr
import py
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def _width_of_current_line(self):
# 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)
Expand Down
4 changes: 3 additions & 1 deletion src/_pytest/warnings.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
Expand Down