typing - switch around node Definition usage

This commit is contained in:
Ronny Pfannschmidt 2024-06-18 16:55:50 +02:00
parent befa0c5e40
commit 988eb6ac9d
4 changed files with 17 additions and 8 deletions

View File

@ -420,7 +420,7 @@ class DoctestTextfile(Module):
# todo: this shouldnt be a module
obj: None = None # type: ignore[assignment]
def collect(self) -> Iterable[DoctestItem]:
def collect(self) -> Iterable[DoctestItem]: # type: ignore[override]
import doctest
# Inspired by doctest.testfile; ideally we would use it directly,
@ -498,7 +498,7 @@ def _patch_unwrap_mock_aware() -> Generator[None, None, None]:
class DoctestModule(Module):
def collect(self) -> Iterable[DoctestItem]:
def collect(self) -> Iterable[DoctestItem]: # type: ignore[override]
import doctest
class MockAwareDocTestFinder(doctest.DocTestFinder):

View File

@ -655,6 +655,11 @@ class Directory(FSCollector, abc.ABC):
"""
class Definition(Collector, abc.ABC):
@abc.abstractmethod
def collect(self) -> Iterable[Item]: ...
class Item(Node, abc.ABC):
"""Base class of all test invocation items.

View File

@ -402,7 +402,7 @@ class PyCollector(PyobjMixin, nodes.Collector, abc.ABC):
return True
return False
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
def collect(self) -> Iterable[nodes.Definition | nodes.Collector]:
if not getattr(self.obj, "__test__", True):
return []
@ -415,10 +415,10 @@ class PyCollector(PyobjMixin, nodes.Collector, abc.ABC):
# In each class, nodes should be definition ordered.
# __dict__ is definition ordered.
seen: set[str] = set()
dict_values: list[list[nodes.Item | nodes.Collector]] = []
dict_values: list[list[nodes.Definition | nodes.Collector]] = []
ihook = self.ihook
for dic in dicts:
values: list[nodes.Item | nodes.Collector] = []
values: list[nodes.Definition | nodes.Collector] = []
# Note: seems like the dict can change during iteration -
# be careful not to remove the list() without consideration.
for name, obj in list(dic.items()):
@ -564,7 +564,7 @@ class Module(nodes.File, PyCollector):
def _getobj(self) -> types.ModuleType:
return importtestmodule(self.path, self.config)
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
def collect(self) -> Iterable[nodes.Collector]:
self._register_setup_module_fixture()
self._register_setup_function_fixture()
self.session._fixturemanager.parsefactories(self)
@ -771,7 +771,7 @@ class Class(PyCollector):
def newinstance(self) -> Any:
return self.obj()
def collect(self) -> Iterable[nodes.Item | nodes.Collector]:
def collect(self) -> Iterable[nodes.Collector]:
if not safe_getattr(self.obj, "__test__", True):
return []
if hasinit(self.obj):
@ -1132,6 +1132,8 @@ class Metafunc:
test function is defined.
"""
definition: FunctionDefinition
def __init__(
self,
definition: FunctionDefinition,
@ -1708,6 +1710,8 @@ class FunctionDefinition(Function):
"""This class is a stop gap solution until we evolve to have actual function
definition nodes and manage to get rid of ``metafunc``."""
parent: Module | Class
def runtest(self) -> None:
raise RuntimeError("function definitions are not supposed to be run as tests")

View File

@ -81,7 +81,7 @@ class UnitTestCase(Class):
# it.
return self.obj("runTest")
def collect(self) -> Iterable[Item | Collector]:
def collect(self) -> Iterable[Item | Collector]: # type: ignore[override]
from unittest import TestLoader
cls = self.obj