@@ -656,20 +656,6 @@ class Item(Node):
656656
657657 nextitem = None
658658
659- def __init_subclass__ (cls ) -> None :
660- problems = ", " .join (
661- base .__name__ for base in cls .__bases__ if issubclass (base , Collector )
662- )
663- if problems :
664- warnings .warn (
665- f"{ cls .__name__ } is an Item subclass and should not be a collector, "
666- f"however its bases { problems } are collectors.\n "
667- "Please split the Collectors and the Item into separate node types.\n "
668- "Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html\n "
669- "example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/" ,
670- PytestWarning ,
671- )
672-
673659 def __init__ (
674660 self ,
675661 name ,
@@ -697,6 +683,37 @@ def __init__(
697683 #: for this test.
698684 self .user_properties : List [Tuple [str , object ]] = []
699685
686+ self ._check_item_and_collector_diamond_inheritance ()
687+
688+ def _check_item_and_collector_diamond_inheritance (self ) -> None :
689+ """
690+ Check if the current type inherits from both File and Collector
691+ at the same time, emitting a warning accordingly (#8447).
692+ """
693+ cls = type (self )
694+
695+ # We inject an attribute in the type to avoid issuing this warning
696+ # for the same class more than once, which is not helpful.
697+ # It is a hack, but was deemed acceptable in order to avoid
698+ # flooding the user in the common case.
699+ attr_name = "_pytest_diamond_inheritance_warning_shown"
700+ if getattr (cls , attr_name , False ):
701+ return
702+ setattr (cls , attr_name , True )
703+
704+ problems = ", " .join (
705+ base .__name__ for base in cls .__bases__ if issubclass (base , Collector )
706+ )
707+ if problems :
708+ warnings .warn (
709+ f"{ cls .__name__ } is an Item subclass and should not be a collector, "
710+ f"however its bases { problems } are collectors.\n "
711+ "Please split the Collectors and the Item into separate node types.\n "
712+ "Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html\n "
713+ "example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/" ,
714+ PytestWarning ,
715+ )
716+
700717 def runtest (self ) -> None :
701718 """Run the test case for this item.
702719
0 commit comments