Skip to content

Commit 8430b21

Browse files
committed
Type annotate main.py and some parts related to collection
1 parent b4ace46 commit 8430b21

File tree

9 files changed

+199
-85
lines changed

9 files changed

+199
-85
lines changed

src/_pytest/config/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def __init__(self, pluginmanager, *, invocation_params=None) -> None:
773773
)
774774

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

src/_pytest/doctest.py

+11-5
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):
@@ -361,7 +367,7 @@ def _get_continue_on_failure(config):
361367
class DoctestTextfile(pytest.Module):
362368
obj = None
363369

364-
def collect(self):
370+
def collect(self) -> Iterable[DoctestItem]:
365371
import doctest
366372

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

441447

442448
class DoctestModule(pytest.Module):
443-
def collect(self):
449+
def collect(self) -> Iterable[DoctestItem]:
444450
import doctest
445451

446452
class MockAwareDocTestFinder(doctest.DocTestFinder):

src/_pytest/hookspec.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
22
from typing import Any
3+
from typing import List
34
from typing import Optional
5+
from typing import Union
46

7+
import py.path
58
from pluggy import HookspecMarker
69

710
from _pytest.compat import TYPE_CHECKING
811

912
if TYPE_CHECKING:
1013
from _pytest.main import Session
14+
from _pytest.nodes import Collector
15+
from _pytest.nodes import Item
16+
from _pytest.python import Module
17+
from _pytest.python import PyCollector
1118

1219

1320
hookspec = HookspecMarker("pytest")
@@ -215,7 +222,7 @@ def pytest_collect_directory(path, parent):
215222
"""
216223

217224

218-
def pytest_collect_file(path, parent):
225+
def pytest_collect_file(path: py.path.local, parent) -> "Optional[Collector]":
219226
""" return collection Node or None for the given path. Any new node
220227
needs to have the specified ``parent`` as a parent.
221228
@@ -255,7 +262,7 @@ def pytest_make_collect_report(collector):
255262

256263

257264
@hookspec(firstresult=True)
258-
def pytest_pycollect_makemodule(path, parent):
265+
def pytest_pycollect_makemodule(path: py.path.local, parent) -> "Optional[Module]":
259266
""" return a Module collector or None for the given path.
260267
This hook will be called for each matching test module path.
261268
The pytest_collect_file hook needs to be used if you want to
@@ -268,7 +275,9 @@ def pytest_pycollect_makemodule(path, parent):
268275

269276

270277
@hookspec(firstresult=True)
271-
def pytest_pycollect_makeitem(collector, name, obj):
278+
def pytest_pycollect_makeitem(
279+
collector: "PyCollector", name: str, obj
280+
) -> "Union[None, Item, Collector, List[Union[Item, Collector]]]":
272281
""" return custom item/collector for a python object in a module, or None.
273282
274283
Stops at first non-None result, see :ref:`firstresult` """

0 commit comments

Comments
 (0)