Skip to content

Commit eb648ab

Browse files
committed
Type annotate main.py and some parts related to collection
1 parent e440b43 commit eb648ab

File tree

9 files changed

+220
-93
lines changed

9 files changed

+220
-93
lines changed

src/_pytest/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def __init__(self, pluginmanager, *, invocation_params=None) -> None:
771771
)
772772

773773
@property
774-
def invocation_dir(self):
774+
def invocation_dir(self) -> py.path.local:
775775
"""Backward compatibility"""
776776
return py.path.local(str(self.invocation_params.dir))
777777

src/_pytest/doctest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import warnings
88
from contextlib import contextmanager
99
from typing import Dict
10+
from typing import Iterable
1011
from typing import List
1112
from typing import Optional
1213
from typing import Sequence
@@ -108,13 +109,18 @@ def pytest_unconfigure():
108109
RUNNER_CLASS = None
109110

110111

111-
def pytest_collect_file(path, parent):
112+
def pytest_collect_file(
113+
path: py.path.local, parent
114+
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
112115
config = parent.config
113116
if path.ext == ".py":
114117
if config.option.doctestmodules and not _is_setup_py(config, path, parent):
115-
return DoctestModule.from_parent(parent, fspath=path)
118+
mod = DoctestModule.from_parent(parent, fspath=path) # type: DoctestModule
119+
return mod
116120
elif _is_doctest(config, path, parent):
117-
return DoctestTextfile.from_parent(parent, fspath=path)
121+
txt = DoctestTextfile.from_parent(parent, fspath=path) # type: DoctestTextfile
122+
return txt
123+
return None
118124

119125

120126
def _is_setup_py(config, path, parent):
@@ -355,7 +361,7 @@ def _get_continue_on_failure(config):
355361
class DoctestTextfile(pytest.Module):
356362
obj = None
357363

358-
def collect(self):
364+
def collect(self) -> Iterable[DoctestItem]:
359365
import doctest
360366

361367
# inspired by doctest.testfile; ideally we would use it directly,
@@ -434,7 +440,7 @@ def _mock_aware_unwrap(obj, stop=None):
434440

435441

436442
class DoctestModule(pytest.Module):
437-
def collect(self):
443+
def collect(self) -> Iterable[DoctestItem]:
438444
import doctest
439445

440446
class MockAwareDocTestFinder(doctest.DocTestFinder):

src/_pytest/hookspec.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
2+
from typing import List
3+
from typing import Optional
4+
from typing import Union
5+
6+
import py.path
27
from pluggy import HookspecMarker
38

9+
from _pytest.compat import TYPE_CHECKING
10+
11+
if TYPE_CHECKING:
12+
from _pytest.nodes import Collector
13+
from _pytest.nodes import Item
14+
from _pytest.python import Module
15+
from _pytest.python import PyCollector
16+
417

518
hookspec = HookspecMarker("pytest")
619

@@ -207,7 +220,7 @@ def pytest_collect_directory(path, parent):
207220
"""
208221

209222

210-
def pytest_collect_file(path, parent):
223+
def pytest_collect_file(path: py.path.local, parent) -> "Optional[Collector]":
211224
""" return collection Node or None for the given path. Any new node
212225
needs to have the specified ``parent`` as a parent.
213226
@@ -247,7 +260,7 @@ def pytest_make_collect_report(collector):
247260

248261

249262
@hookspec(firstresult=True)
250-
def pytest_pycollect_makemodule(path, parent):
263+
def pytest_pycollect_makemodule(path: py.path.local, parent) -> "Optional[Module]":
251264
""" return a Module collector or None for the given path.
252265
This hook will be called for each matching test module path.
253266
The pytest_collect_file hook needs to be used if you want to
@@ -260,7 +273,9 @@ def pytest_pycollect_makemodule(path, parent):
260273

261274

262275
@hookspec(firstresult=True)
263-
def pytest_pycollect_makeitem(collector, name, obj):
276+
def pytest_pycollect_makeitem(
277+
collector: "PyCollector", name: str, obj
278+
) -> "Union[None, Item, Collector, List[Union[Item, Collector]]]":
264279
""" return custom item/collector for a python object in a module, or None.
265280
266281
Stops at first non-None result, see :ref:`firstresult` """

0 commit comments

Comments
 (0)