@@ -656,20 +656,6 @@ class Item(Node):
656
656
657
657
nextitem = None
658
658
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
-
673
659
def __init__ (
674
660
self ,
675
661
name ,
@@ -697,6 +683,37 @@ def __init__(
697
683
#: for this test.
698
684
self .user_properties : List [Tuple [str , object ]] = []
699
685
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
+
700
717
def runtest (self ) -> None :
701
718
"""Run the test case for this item.
702
719
0 commit comments