From 988eb6ac9d6f70cacedd0a40f87ddddaef35f959 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 18 Jun 2024 16:55:50 +0200 Subject: [PATCH] typing - switch around node Definition usage --- src/_pytest/doctest.py | 4 ++-- src/_pytest/nodes.py | 5 +++++ src/_pytest/python.py | 14 +++++++++----- src/_pytest/unittest.py | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 6b57b35a9..c0fac8aa5 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -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): diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 78c37331c..3076484a3 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -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. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 62f605360..9a0141e4c 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -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") diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index aefea1333..e1202aaa5 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -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