Merge pull request #11677 from bluetech/nodes-abc

nodes,python: mark abstract node classes as ABCs
This commit is contained in:
Ran Benita
2023-12-10 09:41:46 +02:00
committed by GitHub
9 changed files with 39 additions and 12 deletions

View File

@@ -135,7 +135,9 @@ def get_scope_node(
import _pytest.python
if scope is Scope.Function:
return node.getparent(nodes.Item)
# Type ignored because this is actually safe, see:
# https://github.com/python/mypy/issues/4717
return node.getparent(nodes.Item) # type: ignore[type-abstract]
elif scope is Scope.Class:
return node.getparent(_pytest.python.Class)
elif scope is Scope.Module:

View File

@@ -1,3 +1,4 @@
import abc
import os
import warnings
from functools import cached_property
@@ -121,7 +122,7 @@ def _imply_path(
_NodeType = TypeVar("_NodeType", bound="Node")
class NodeMeta(type):
class NodeMeta(abc.ABCMeta):
"""Metaclass used by :class:`Node` to enforce that direct construction raises
:class:`Failed`.
@@ -165,7 +166,7 @@ class NodeMeta(type):
return super().__call__(*k, **known_kw)
class Node(metaclass=NodeMeta):
class Node(abc.ABC, metaclass=NodeMeta):
r"""Base class of :class:`Collector` and :class:`Item`, the components of
the test collection tree.
@@ -534,7 +535,7 @@ def get_fslocation_from_item(node: "Node") -> Tuple[Union[str, Path], Optional[i
return getattr(node, "fspath", "unknown location"), -1
class Collector(Node):
class Collector(Node, abc.ABC):
"""Base class of all collectors.
Collector create children through `collect()` and thus iteratively build
@@ -544,6 +545,7 @@ class Collector(Node):
class CollectError(Exception):
"""An error during collection, contains a custom message."""
@abc.abstractmethod
def collect(self) -> Iterable[Union["Item", "Collector"]]:
"""Collect children (items and collectors) for this collector."""
raise NotImplementedError("abstract")
@@ -588,7 +590,7 @@ def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[
return None
class FSCollector(Collector):
class FSCollector(Collector, abc.ABC):
"""Base class for filesystem collectors."""
def __init__(
@@ -666,14 +668,14 @@ class FSCollector(Collector):
return self.session.isinitpath(path)
class File(FSCollector):
class File(FSCollector, abc.ABC):
"""Base class for collecting tests from a file.
:ref:`non-python tests`.
"""
class Item(Node):
class Item(Node, abc.ABC):
"""Base class of all test invocation items.
Note that for a single function there might be multiple test invocation items.
@@ -739,6 +741,7 @@ class Item(Node):
PytestWarning,
)
@abc.abstractmethod
def runtest(self) -> None:
"""Run the test case for this item.

View File

@@ -1,4 +1,5 @@
"""Python test discovery, setup and run of test functions."""
import abc
import dataclasses
import enum
import fnmatch
@@ -380,7 +381,7 @@ del _EmptyClass
# fmt: on
class PyCollector(PyobjMixin, nodes.Collector):
class PyCollector(PyobjMixin, nodes.Collector, abc.ABC):
def funcnamefilter(self, name: str) -> bool:
return self._matches_prefix_or_glob_option("python_functions", name)