Allow File.from_parent to forward custom parameters to the constructor

This commit is contained in:
Bruno Oliveira 2020-05-01 10:58:47 -03:00
parent be68496440
commit 7f5978c34c
3 changed files with 24 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix ``File.from_constructor`` so it forwards extra keyword arguments to the constructor.

View File

@ -483,11 +483,11 @@ class FSCollector(Collector):
self._norecursepatterns = self.config.getini("norecursedirs") self._norecursepatterns = self.config.getini("norecursedirs")
@classmethod @classmethod
def from_parent(cls, parent, *, fspath): def from_parent(cls, parent, *, fspath, **kw):
""" """
The public constructor The public constructor
""" """
return super().from_parent(parent=parent, fspath=fspath) return super().from_parent(parent=parent, fspath=fspath, **kw)
def _gethookproxy(self, fspath: py.path.local): def _gethookproxy(self, fspath: py.path.local):
# check if we have the common case of running # check if we have the common case of running

View File

@ -1332,3 +1332,24 @@ def test_does_not_put_src_on_path(testdir):
) )
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret == ExitCode.OK assert result.ret == ExitCode.OK
def test_fscollector_from_parent(tmpdir, request):
"""Ensure File.from_parent can forward custom arguments to the constructor.
Context: https://github.com/pytest-dev/pytest-cpp/pull/47
"""
class MyCollector(pytest.File):
def __init__(self, fspath, parent, x):
super().__init__(fspath, parent)
self.x = x
@classmethod
def from_parent(cls, parent, *, fspath, x):
return super().from_parent(parent=parent, fspath=fspath, x=x)
collector = MyCollector.from_parent(
parent=request.session, fspath=tmpdir / "foo", x=10
)
assert collector.x == 10