Skip to content

Commit 88b8003

Browse files
committed
typing: pytest_collection
1 parent eb5e651 commit 88b8003

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

src/_pytest/assertion/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from _pytest.assertion import rewrite
88
from _pytest.assertion import truncate
99
from _pytest.assertion import util
10+
from _pytest.compat import TYPE_CHECKING
11+
12+
if TYPE_CHECKING:
13+
from _pytest.main import Session
1014

1115

1216
def pytest_addoption(parser):
@@ -91,7 +95,7 @@ def undo():
9195
return hook
9296

9397

94-
def pytest_collection(session):
98+
def pytest_collection(session: "Session") -> None:
9599
# this hook is only called when test modules are collected
96100
# so for example not in the master process of pytest-xdist
97101
# (which does not collect test modules)

src/_pytest/hookspec.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
2+
from typing import Any
3+
from typing import Optional
4+
25
from pluggy import HookspecMarker
36

7+
from _pytest.compat import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
from _pytest.main import Session
11+
412

513
hookspec = HookspecMarker("pytest")
614

@@ -158,7 +166,7 @@ def pytest_load_initial_conftests(early_config, parser, args):
158166

159167

160168
@hookspec(firstresult=True)
161-
def pytest_collection(session):
169+
def pytest_collection(session: "Session") -> Optional[Any]:
162170
"""Perform the collection protocol for the given session.
163171
164172
Stops at first non-None result, see :ref:`firstresult`.

src/_pytest/logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import StringIO
66
from typing import AbstractSet
77
from typing import Dict
8+
from typing import Generator
89
from typing import List
910
from typing import Mapping
1011

@@ -591,7 +592,7 @@ def _log_cli_enabled(self):
591592
) is not None or self._config.getini("log_cli")
592593

593594
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
594-
def pytest_collection(self):
595+
def pytest_collection(self) -> Generator[None, None, None]:
595596
with self.live_logs_context():
596597
if self.log_cli_handler:
597598
self.log_cli_handler.set_when("collection")

src/_pytest/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from typing import Dict
99
from typing import FrozenSet
1010
from typing import List
11+
from typing import Optional
12+
from typing import Union
1113

1214
import attr
1315
import py
@@ -249,7 +251,7 @@ def pytest_cmdline_main(config):
249251
return wrap_session(config, _main)
250252

251253

252-
def _main(config, session):
254+
def _main(config: Config, session: "Session") -> Optional[Union[int, ExitCode]]:
253255
""" default command line protocol for initialization, session,
254256
running tests and reporting. """
255257
config.hook.pytest_collection(session=session)
@@ -259,6 +261,7 @@ def _main(config, session):
259261
return ExitCode.TESTS_FAILED
260262
elif session.testscollected == 0:
261263
return ExitCode.NO_TESTS_COLLECTED
264+
return None
262265

263266

264267
def pytest_collection(session):

src/_pytest/terminal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def _width_of_current_line(self):
514514
# py < 1.6.0
515515
return self._tw.chars_on_current_line
516516

517-
def pytest_collection(self):
517+
def pytest_collection(self) -> None:
518518
if self.isatty:
519519
if self.config.option.verbose >= 0:
520520
self.write("collecting ... ", bold=True)

src/_pytest/warnings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import sys
22
import warnings
33
from contextlib import contextmanager
4+
from typing import Generator
45

56
import pytest
7+
from _pytest.main import Session
68

79

810
def _setoption(wmod, arg):
@@ -117,7 +119,7 @@ def pytest_runtest_protocol(item):
117119

118120

119121
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
120-
def pytest_collection(session):
122+
def pytest_collection(session: Session) -> Generator[None, None, None]:
121123
config = session.config
122124
with catch_warnings_for_item(
123125
config=config, ihook=config.hook, when="collect", item=None

0 commit comments

Comments
 (0)