27
27
from typing import Sequence
28
28
from typing import Set
29
29
from typing import Tuple
30
+ from typing import TYPE_CHECKING
30
31
from typing import Union
31
32
import warnings
32
33
81
82
from _pytest .warning_types import PytestUnhandledCoroutineWarning
82
83
83
84
85
+ if TYPE_CHECKING :
86
+ from typing import Self
87
+
88
+
84
89
_PYTEST_DIR = Path (_pytest .__file__ ).parent
85
90
86
91
@@ -204,8 +209,7 @@ def pytest_collect_directory(
204
209
) -> Optional [nodes .Collector ]:
205
210
pkginit = path / "__init__.py"
206
211
if pkginit .is_file ():
207
- pkg : Package = Package .from_parent (parent , path = path )
208
- return pkg
212
+ return Package .from_parent (parent , path = path )
209
213
return None
210
214
211
215
@@ -230,8 +234,7 @@ def path_matches_patterns(path: Path, patterns: Iterable[str]) -> bool:
230
234
231
235
232
236
def pytest_pycollect_makemodule (module_path : Path , parent ) -> "Module" :
233
- mod : Module = Module .from_parent (parent , path = module_path )
234
- return mod
237
+ return Module .from_parent (parent , path = module_path )
235
238
236
239
237
240
@hookimpl (trylast = True )
@@ -242,8 +245,7 @@ def pytest_pycollect_makeitem(
242
245
# Nothing was collected elsewhere, let's do it here.
243
246
if safe_isclass (obj ):
244
247
if collector .istestclass (obj , name ):
245
- klass : Class = Class .from_parent (collector , name = name , obj = obj )
246
- return klass
248
+ return Class .from_parent (collector , name = name , obj = obj )
247
249
elif collector .istestfunction (obj , name ):
248
250
# mock seems to store unbound methods (issue473), normalize it.
249
251
obj = getattr (obj , "__func__" , obj )
@@ -262,7 +264,7 @@ def pytest_pycollect_makeitem(
262
264
)
263
265
elif getattr (obj , "__test__" , True ):
264
266
if is_generator (obj ):
265
- res : Function = Function .from_parent (collector , name = name )
267
+ res = Function .from_parent (collector , name = name )
266
268
reason = (
267
269
f"yield tests were removed in pytest 4.0 - { name } will be ignored"
268
270
)
@@ -465,9 +467,7 @@ def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]:
465
467
clscol = self .getparent (Class )
466
468
cls = clscol and clscol .obj or None
467
469
468
- definition : FunctionDefinition = FunctionDefinition .from_parent (
469
- self , name = name , callobj = funcobj
470
- )
470
+ definition = FunctionDefinition .from_parent (self , name = name , callobj = funcobj )
471
471
fixtureinfo = definition ._fixtureinfo
472
472
473
473
# pytest_generate_tests impls call metafunc.parametrize() which fills
@@ -751,7 +751,7 @@ class Class(PyCollector):
751
751
"""Collector for test methods (and nested classes) in a Python class."""
752
752
753
753
@classmethod
754
- def from_parent (cls , parent , * , name , obj = None , ** kw ):
754
+ def from_parent (cls , parent , * , name , obj = None , ** kw ) -> "Self" : # type: ignore[override]
755
755
"""The public constructor."""
756
756
return super ().from_parent (name = name , parent = parent , ** kw )
757
757
@@ -1730,8 +1730,9 @@ def __init__(
1730
1730
self .fixturenames = fixtureinfo .names_closure
1731
1731
self ._initrequest ()
1732
1732
1733
+ # todo: determine sound type limitations
1733
1734
@classmethod
1734
- def from_parent (cls , parent , ** kw ): # todo: determine sound type limitations
1735
+ def from_parent (cls , parent , ** kw ) -> "Self" :
1735
1736
"""The public constructor."""
1736
1737
return super ().from_parent (parent = parent , ** kw )
1737
1738
0 commit comments