@@ -247,7 +247,9 @@ def runtest(self) -> None:
247247 # TODO: add a better error message for when someone uses skip and xfail at the same time
248248 elif self .xfail :
249249 self .add_marker (pytest .mark .xfail )
250- suite = self .parent .obj ()
250+ parent = self .getparent (DataSuiteCollector )
251+ assert parent is not None , 'Should not happen'
252+ suite = parent .obj ()
251253 suite .setup ()
252254 try :
253255 suite .run_case (self )
@@ -550,12 +552,12 @@ def pytest_pycollect_makeitem(collector: Any, name: str,
550552 # The collect method of the returned DataSuiteCollector instance will be called later,
551553 # with self.obj being obj.
552554 return DataSuiteCollector .from_parent ( # type: ignore[no-untyped-call]
553- parent = collector , name = name
555+ parent = collector , name = name ,
554556 )
555557 return None
556558
557559
558- def split_test_cases (parent : 'DataSuiteCollector ' , suite : 'DataSuite' ,
560+ def split_test_cases (parent : 'DataFileCollector ' , suite : 'DataSuite' ,
559561 file : str ) -> Iterator ['DataDrivenTestCase' ]:
560562 """Iterate over raw test cases in file, at collection time, ignoring sub items.
561563
@@ -596,7 +598,7 @@ def split_test_cases(parent: 'DataSuiteCollector', suite: 'DataSuite',
596598
597599
598600class DataSuiteCollector (pytest .Class ):
599- def collect (self ) -> Iterator [pytest . Item ]:
601+ def collect (self ) -> Iterator ['DataFileCollector' ]:
600602 """Called by pytest on each of the object returned from pytest_pycollect_makeitem"""
601603
602604 # obj is the object for which pytest_pycollect_makeitem returned self.
@@ -605,8 +607,32 @@ def collect(self) -> Iterator[pytest.Item]:
605607 assert os .path .isdir (suite .data_prefix ), \
606608 'Test data prefix ({}) not set correctly' .format (suite .data_prefix )
607609
608- for f in suite .files :
609- yield from split_test_cases (self , suite , os .path .join (suite .data_prefix , f ))
610+ for data_file in suite .files :
611+ yield DataFileCollector .from_parent (parent = self , name = data_file )
612+
613+
614+ class DataFileCollector (pytest .Collector ):
615+ """Represents a single `.test` data driven test file.
616+
617+ More context: https://github.com/python/mypy/issues/11662
618+ """
619+ parent : DataSuiteCollector
620+
621+ @classmethod # We have to fight with pytest here:
622+ def from_parent ( # type: ignore[override]
623+ cls ,
624+ parent : DataSuiteCollector ,
625+ * ,
626+ name : str ,
627+ ) -> 'DataFileCollector' :
628+ return super ().from_parent (parent , name = name )
629+
630+ def collect (self ) -> Iterator ['DataDrivenTestCase' ]:
631+ yield from split_test_cases (
632+ parent = self ,
633+ suite = self .parent .obj ,
634+ file = os .path .join (self .parent .obj .data_prefix , self .name ),
635+ )
610636
611637
612638def add_test_name_suffix (name : str , suffix : str ) -> str :
0 commit comments