@@ -190,8 +190,8 @@ def path_matches_patterns(path, patterns):
190190
191191def pytest_pycollect_makemodule (path , parent ):
192192 if path .basename == "__init__.py" :
193- return Package ( path , parent )
194- return Module ( path , parent )
193+ return Package . from_parent ( parent , fspath = path )
194+ return Module . from_parent ( parent , fspath = path )
195195
196196
197197@hookimpl (hookwrapper = True )
@@ -203,7 +203,7 @@ def pytest_pycollect_makeitem(collector, name, obj):
203203 # nothing was collected elsewhere, let's do it here
204204 if safe_isclass (obj ):
205205 if collector .istestclass (obj , name ):
206- outcome .force_result (Class ( name , parent = collector ))
206+ outcome .force_result (Class . from_parent ( collector , name = name , obj = obj ))
207207 elif collector .istestfunction (obj , name ):
208208 # mock seems to store unbound methods (issue473), normalize it
209209 obj = getattr (obj , "__func__" , obj )
@@ -222,7 +222,7 @@ def pytest_pycollect_makeitem(collector, name, obj):
222222 )
223223 elif getattr (obj , "__test__" , True ):
224224 if is_generator (obj ):
225- res = Function ( name , parent = collector )
225+ res = Function . from_parent ( collector , name = name )
226226 reason = "yield tests were removed in pytest 4.0 - {name} will be ignored" .format (
227227 name = name
228228 )
@@ -381,7 +381,7 @@ def _genfunctions(self, name, funcobj):
381381 cls = clscol and clscol .obj or None
382382 fm = self .session ._fixturemanager
383383
384- definition = FunctionDefinition ( name = name , parent = self , callobj = funcobj )
384+ definition = FunctionDefinition . from_parent ( self , name = name , callobj = funcobj )
385385 fixtureinfo = fm .getfixtureinfo (definition , funcobj , cls )
386386
387387 metafunc = Metafunc (
@@ -396,7 +396,7 @@ def _genfunctions(self, name, funcobj):
396396 self .ihook .pytest_generate_tests .call_extra (methods , dict (metafunc = metafunc ))
397397
398398 if not metafunc ._calls :
399- yield Function ( name , parent = self , fixtureinfo = fixtureinfo )
399+ yield Function . from_parent ( self , name = name , fixtureinfo = fixtureinfo )
400400 else :
401401 # add funcargs() as fixturedefs to fixtureinfo.arg2fixturedefs
402402 fixtures .add_funcarg_pseudo_fixture_def (self , metafunc , fm )
@@ -408,9 +408,9 @@ def _genfunctions(self, name, funcobj):
408408
409409 for callspec in metafunc ._calls :
410410 subname = "{}[{}]" .format (name , callspec .id )
411- yield Function (
411+ yield Function .from_parent (
412+ self ,
412413 name = subname ,
413- parent = self ,
414414 callspec = callspec ,
415415 callobj = funcobj ,
416416 fixtureinfo = fixtureinfo ,
@@ -626,7 +626,7 @@ def collect(self):
626626 if init_module .check (file = 1 ) and path_matches_patterns (
627627 init_module , self .config .getini ("python_files" )
628628 ):
629- yield Module ( init_module , self )
629+ yield Module . from_parent ( self , fspath = init_module )
630630 pkg_prefixes = set ()
631631 for path in this_path .visit (rec = self ._recurse , bf = True , sort = True ):
632632 # We will visit our own __init__.py file, in which case we skip it.
@@ -677,6 +677,10 @@ def _get_first_non_fixture_func(obj, names):
677677class Class (PyCollector ):
678678 """ Collector for test methods. """
679679
680+ @classmethod
681+ def from_parent (cls , parent , * , name , obj = None ):
682+ return cls ._create (name = name , parent = parent )
683+
680684 def collect (self ):
681685 if not safe_getattr (self .obj , "__test__" , True ):
682686 return []
@@ -702,7 +706,7 @@ def collect(self):
702706 self ._inject_setup_class_fixture ()
703707 self ._inject_setup_method_fixture ()
704708
705- return [Instance ( name = "()" , parent = self )]
709+ return [Instance . from_parent ( self , name = "()" )]
706710
707711 def _inject_setup_class_fixture (self ):
708712 """Injects a hidden autouse, class scoped fixture into the collected class object
@@ -1454,6 +1458,10 @@ def __init__(
14541458 #: .. versionadded:: 3.0
14551459 self .originalname = originalname
14561460
1461+ @classmethod
1462+ def from_parent (cls , parent , ** kw ):
1463+ return cls ._create (parent = parent , ** kw )
1464+
14571465 def _initrequest (self ):
14581466 self .funcargs = {}
14591467 self ._request = fixtures .FixtureRequest (self )
0 commit comments