Skip to content

Allow File.from_parent to forward custom parameters to the constructor #7143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7143.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``File.from_constructor`` so it forwards extra keyword arguments to the constructor.
4 changes: 2 additions & 2 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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