Skip to content

Commit 8cfc5a1

Browse files
[WIP] add type annotations to from_parents and its usages, BROKEN
1 parent 8ba0b7b commit 8cfc5a1

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/_pytest/doctest.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import traceback
77
import warnings
88
from contextlib import contextmanager
9+
from typing import cast
910
from typing import Dict
1011
from typing import List
1112
from typing import Optional
@@ -14,6 +15,7 @@
1415
from typing import Union
1516

1617
import pytest
18+
from _pytest import nodes
1719
from _pytest import outcomes
1820
from _pytest._code.code import ExceptionInfo
1921
from _pytest._code.code import ReprFileLocation
@@ -217,13 +219,21 @@ def __init__(self, name, parent, runner=None, dtest=None):
217219

218220
@classmethod
219221
def from_parent( # type: ignore
220-
cls, parent: "Union[DoctestTextfile, DoctestModule]", *, name, runner, dtest
221-
):
222+
cls: "Type[nodes.N]",
223+
parent: "Union[DoctestTextfile, DoctestModule]",
224+
*,
225+
name,
226+
runner,
227+
dtest
228+
) -> nodes.N:
222229
# incompatible signature due to to imposed limits on sublcass
223230
"""
224231
the public named constructor
225232
"""
226-
return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)
233+
return cast(
234+
nodes.N,
235+
super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest),
236+
)
227237

228238
def setup(self):
229239
if self.dtest is not None:

src/_pytest/nodes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import warnings
33
from functools import lru_cache
44
from typing import Any
5+
from typing import cast
56
from typing import Dict
67
from typing import List
78
from typing import Optional
89
from typing import Set
910
from typing import Tuple
11+
from typing import TypeVar
1012
from typing import Union
1113

1214
import py
@@ -30,6 +32,9 @@
3032
if False: # TYPE_CHECKING
3133
# Imported here due to circular import.
3234
from _pytest.main import Session # noqa: F401
35+
from typing import Type
36+
37+
N = TypeVar("N", bound="Node")
3338

3439
SEP = "/"
3540

@@ -144,7 +149,7 @@ def __init__(
144149
self._nodeid += "::" + self.name
145150

146151
@classmethod
147-
def from_parent(cls, parent: "Node", **kw):
152+
def from_parent(cls: "Type[N]", parent: "Node", **kw) -> N:
148153
"""
149154
Public Constructor for Nodes
150155
@@ -159,7 +164,7 @@ def from_parent(cls, parent: "Node", **kw):
159164
raise TypeError("config is not a valid argument for from_parent")
160165
if "session" in kw:
161166
raise TypeError("session is not a valid argument for from_parent")
162-
return cls._create(parent=parent, **kw)
167+
return cast(N, cls._create(parent=parent, **kw))
163168

164169
@property
165170
def ihook(self):

src/_pytest/python.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
from collections.abc import Sequence
1010
from functools import partial
1111
from textwrap import dedent
12+
from typing import cast
1213
from typing import List
1314
from typing import Optional
1415
from typing import Tuple
16+
from typing import Union
1517

1618
import py
1719

@@ -45,6 +47,9 @@
4547
from _pytest.warning_types import PytestCollectionWarning
4648
from _pytest.warning_types import PytestUnhandledCoroutineWarning
4749

50+
if False: # TYPE_CHECKING
51+
from typing import Type
52+
4853

4954
def pyobj_property(name):
5055
def get(self):
@@ -188,7 +193,7 @@ def path_matches_patterns(path, patterns):
188193
return any(path.fnmatch(pattern) for pattern in patterns)
189194

190195

191-
def pytest_pycollect_makemodule(path, parent):
196+
def pytest_pycollect_makemodule(path, parent) -> Union["Package", "Module"]:
192197
if path.basename == "__init__.py":
193198
return Package.from_parent(parent, fspath=path)
194199
return Module.from_parent(parent, fspath=path)
@@ -678,11 +683,11 @@ class Class(PyCollector):
678683
""" Collector for test methods. """
679684

680685
@classmethod
681-
def from_parent(cls, parent, *, name, obj=None):
686+
def from_parent(cls: "Type[nodes.N]", parent, *, name, obj=None) -> nodes.N:
682687
"""
683688
The public constructor
684689
"""
685-
return super().from_parent(name=name, parent=parent)
690+
return cast(nodes.N, super().from_parent(name=name, parent=parent))
686691

687692
def collect(self):
688693
if not safe_getattr(self.obj, "__test__", True):

testing/python/collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def __call__(self, tmpdir):
277277
)
278278

279279
@staticmethod
280-
def make_function(testdir, **kwargs):
280+
def make_function(testdir, **kwargs) -> pytest.Function:
281281
from _pytest.fixtures import FixtureManager
282282

283283
config = testdir.parseconfigure()

0 commit comments

Comments
 (0)