Skip to content

Commit b18b5a5

Browse files
issue a waring when Items and collectors for a diamond
addresses pytest-dev#8435
1 parent 63bc49d commit b18b5a5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/_pytest/nodes.py

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from _pytest.outcomes import fail
3434
from _pytest.pathlib import absolutepath
3535
from _pytest.store import Store
36+
from _pytest.warning_types import PytestWarning
3637

3738
if TYPE_CHECKING:
3839
# Imported here due to circular import.
@@ -601,6 +602,19 @@ class Item(Node):
601602

602603
nextitem = None
603604

605+
def __init_subclass__(cls):
606+
problems = ", ".join(
607+
base.__name__ for base in cls.__bases__ if issubclass(base, Collector)
608+
)
609+
if problems:
610+
warnings.warn(
611+
f"{cls.__name__} is a Item subclass and should not be a collector.\n"
612+
f"however its bases {problems} are collectors\n"
613+
"please split the collection and the items into 2 node types\n"
614+
"TODO: doc link",
615+
PytestWarning,
616+
)
617+
604618
def __init__(
605619
self,
606620
name,

testing/test_nodes.py

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ def test_node_from_parent_disallowed_arguments() -> None:
3838
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type]
3939

4040

41+
def test_subclassing_node_with_item_warns() -> None:
42+
43+
with pytest.warns(
44+
PytestWarning, match="SoWrong is a Item subclass and should not be a collector"
45+
):
46+
47+
class SoWrong(nodes.Item, nodes.File):
48+
pass
49+
50+
4151
@pytest.mark.parametrize(
4252
"warn_type, msg", [(DeprecationWarning, "deprecated"), (PytestWarning, "pytest")]
4353
)

0 commit comments

Comments
 (0)