Skip to content

Commit 483167c

Browse files
committed
Type annotate main.py and some parts related to collection
1 parent 3fd1ea0 commit 483167c

File tree

9 files changed

+182
-78
lines changed

9 files changed

+182
-78
lines changed

src/_pytest/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def __init__(
806806
self.cache = None # type: Optional[Cache]
807807

808808
@property
809-
def invocation_dir(self):
809+
def invocation_dir(self) -> py.path.local:
810810
"""Backward compatibility"""
811811
return py.path.local(str(self.invocation_params.dir))
812812

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
@@ -109,13 +110,18 @@ def pytest_unconfigure() -> None:
109110
RUNNER_CLASS = None
110111

111112

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

120126

121127
def _is_setup_py(config, path, parent):
@@ -362,7 +368,7 @@ def _get_continue_on_failure(config):
362368
class DoctestTextfile(pytest.Module):
363369
obj = None
364370

365-
def collect(self):
371+
def collect(self) -> Iterable[DoctestItem]:
366372
import doctest
367373

368374
# inspired by doctest.testfile; ideally we would use it directly,
@@ -441,7 +447,7 @@ def _mock_aware_unwrap(obj, stop=None):
441447

442448

443449
class DoctestModule(pytest.Module):
444-
def collect(self):
450+
def collect(self) -> Iterable[DoctestItem]:
445451
import doctest
446452

447453
class MockAwareDocTestFinder(doctest.DocTestFinder):

src/_pytest/hookspec.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Optional
55
from typing import Union
66

7+
import py.path
78
from pluggy import HookspecMarker
89

910
from _pytest.compat import TYPE_CHECKING
@@ -15,7 +16,12 @@
1516
from _pytest.config.argparsing import Parser
1617
from _pytest.config import ExitCode
1718
from _pytest.main import Session
19+
from _pytest.nodes import Collector
20+
from _pytest.nodes import Item
1821
from _pytest.python import Metafunc
22+
from _pytest.python import Module
23+
from _pytest.python import PyCollector
24+
1925

2026
hookspec = HookspecMarker("pytest")
2127

@@ -228,7 +234,7 @@ def pytest_collect_directory(path, parent):
228234
"""
229235

230236

231-
def pytest_collect_file(path, parent):
237+
def pytest_collect_file(path: py.path.local, parent) -> "Optional[Collector]":
232238
""" return collection Node or None for the given path. Any new node
233239
needs to have the specified ``parent`` as a parent.
234240
@@ -268,7 +274,7 @@ def pytest_make_collect_report(collector):
268274

269275

270276
@hookspec(firstresult=True)
271-
def pytest_pycollect_makemodule(path, parent):
277+
def pytest_pycollect_makemodule(path: py.path.local, parent) -> "Optional[Module]":
272278
""" return a Module collector or None for the given path.
273279
This hook will be called for each matching test module path.
274280
The pytest_collect_file hook needs to be used if you want to
@@ -281,7 +287,9 @@ def pytest_pycollect_makemodule(path, parent):
281287

282288

283289
@hookspec(firstresult=True)
284-
def pytest_pycollect_makeitem(collector, name, obj):
290+
def pytest_pycollect_makeitem(
291+
collector: "PyCollector", name: str, obj
292+
) -> "Union[None, Item, Collector, List[Union[Item, Collector]]]":
285293
""" return custom item/collector for a python object in a module, or None.
286294
287295
Stops at first non-None result, see :ref:`firstresult` """

0 commit comments

Comments
 (0)