Skip to content

Commit

Permalink
Allow File.from_parent to forward custom parameters to the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed May 1, 2020
1 parent be68496 commit b2e4e4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/_pytest/nodes.py
Expand Up @@ -483,11 +483,11 @@ def __init__(
self._norecursepatterns = self.config.getini("norecursedirs")

@classmethod
def from_parent(cls, parent, *, fspath):
def from_parent(cls, parent, *, fspath, **kw):
"""
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):
# check if we have the common case of running
Expand Down
21 changes: 21 additions & 0 deletions testing/test_collection.py
Expand Up @@ -1332,3 +1332,24 @@ def test_does_not_put_src_on_path(testdir):
)
result = testdir.runpytest()
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

0 comments on commit b2e4e4d

Please sign in to comment.