Skip to content

Commit 988eb6a

Browse files
typing - switch around node Definition usage
1 parent befa0c5 commit 988eb6a

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

src/_pytest/doctest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class DoctestTextfile(Module):
420420
# todo: this shouldnt be a module
421421
obj: None = None # type: ignore[assignment]
422422

423-
def collect(self) -> Iterable[DoctestItem]:
423+
def collect(self) -> Iterable[DoctestItem]: # type: ignore[override]
424424
import doctest
425425

426426
# Inspired by doctest.testfile; ideally we would use it directly,
@@ -498,7 +498,7 @@ def _mock_aware_unwrap(
498498

499499

500500
class DoctestModule(Module):
501-
def collect(self) -> Iterable[DoctestItem]:
501+
def collect(self) -> Iterable[DoctestItem]: # type: ignore[override]
502502
import doctest
503503

504504
class MockAwareDocTestFinder(doctest.DocTestFinder):

src/_pytest/nodes.py

+5
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ class Directory(FSCollector, abc.ABC):
655655
"""
656656

657657

658+
class Definition(Collector, abc.ABC):
659+
@abc.abstractmethod
660+
def collect(self) -> Iterable[Item]: ...
661+
662+
658663
class Item(Node, abc.ABC):
659664
"""Base class of all test invocation items.
660665

src/_pytest/python.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def _matches_prefix_or_glob_option(self, option_name: str, name: str) -> bool:
402402
return True
403403
return False
404404

405-
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
405+
def collect(self) -> Iterable[nodes.Definition | nodes.Collector]:
406406
if not getattr(self.obj, "__test__", True):
407407
return []
408408

@@ -415,10 +415,10 @@ def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
415415
# In each class, nodes should be definition ordered.
416416
# __dict__ is definition ordered.
417417
seen: set[str] = set()
418-
dict_values: list[list[nodes.Item | nodes.Collector]] = []
418+
dict_values: list[list[nodes.Definition | nodes.Collector]] = []
419419
ihook = self.ihook
420420
for dic in dicts:
421-
values: list[nodes.Item | nodes.Collector] = []
421+
values: list[nodes.Definition | nodes.Collector] = []
422422
# Note: seems like the dict can change during iteration -
423423
# be careful not to remove the list() without consideration.
424424
for name, obj in list(dic.items()):
@@ -564,7 +564,7 @@ class Module(nodes.File, PyCollector):
564564
def _getobj(self) -> types.ModuleType:
565565
return importtestmodule(self.path, self.config)
566566

567-
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
567+
def collect(self) -> Iterable[nodes.Collector]:
568568
self._register_setup_module_fixture()
569569
self._register_setup_function_fixture()
570570
self.session._fixturemanager.parsefactories(self)
@@ -771,7 +771,7 @@ def from_parent(cls, parent: nodes.Node, *, name: str, **kw: Any) -> Self: # ty
771771
def newinstance(self) -> Any:
772772
return self.obj()
773773

774-
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
774+
def collect(self) -> Iterable[nodes.Collector]:
775775
if not safe_getattr(self.obj, "__test__", True):
776776
return []
777777
if hasinit(self.obj):
@@ -1132,6 +1132,8 @@ class Metafunc:
11321132
test function is defined.
11331133
"""
11341134

1135+
definition: FunctionDefinition
1136+
11351137
def __init__(
11361138
self,
11371139
definition: FunctionDefinition,
@@ -1708,6 +1710,8 @@ class FunctionDefinition(Function):
17081710
"""This class is a stop gap solution until we evolve to have actual function
17091711
definition nodes and manage to get rid of ``metafunc``."""
17101712

1713+
parent: Module | Class
1714+
17111715
def runtest(self) -> None:
17121716
raise RuntimeError("function definitions are not supposed to be run as tests")
17131717

src/_pytest/unittest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def newinstance(self):
8181
# it.
8282
return self.obj("runTest")
8383

84-
def collect(self) -> Iterable[Item | Collector]:
84+
def collect(self) -> Iterable[Item | Collector]: # type: ignore[override]
8585
from unittest import TestLoader
8686

8787
cls = self.obj

0 commit comments

Comments
 (0)