Skip to content

Commit 3256288

Browse files
authored
Merge pull request #7146 from nicoddemus/backport-7143
2 parents 9aed656 + 2a0bbfe commit 3256288

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

changelog/7143.bugfix.rst

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

src/_pytest/nodes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,11 @@ def __init__(
474474
self._norecursepatterns = self.config.getini("norecursedirs")
475475

476476
@classmethod
477-
def from_parent(cls, parent, *, fspath):
477+
def from_parent(cls, parent, *, fspath, **kw):
478478
"""
479479
The public constructor
480480
"""
481-
return super().from_parent(parent=parent, fspath=fspath)
481+
return super().from_parent(parent=parent, fspath=fspath, **kw)
482482

483483
def _gethookproxy(self, fspath: py.path.local):
484484
# check if we have the common case of running

testing/test_collection.py

+21
Original file line numberDiff line numberDiff line change
@@ -1332,3 +1332,24 @@ def test_does_not_put_src_on_path(testdir):
13321332
)
13331333
result = testdir.runpytest()
13341334
assert result.ret == ExitCode.OK
1335+
1336+
1337+
def test_fscollector_from_parent(tmpdir, request):
1338+
"""Ensure File.from_parent can forward custom arguments to the constructor.
1339+
1340+
Context: https://github.com/pytest-dev/pytest-cpp/pull/47
1341+
"""
1342+
1343+
class MyCollector(pytest.File):
1344+
def __init__(self, fspath, parent, x):
1345+
super().__init__(fspath, parent)
1346+
self.x = x
1347+
1348+
@classmethod
1349+
def from_parent(cls, parent, *, fspath, x):
1350+
return super().from_parent(parent=parent, fspath=fspath, x=x)
1351+
1352+
collector = MyCollector.from_parent(
1353+
parent=request.session, fspath=tmpdir / "foo", x=10
1354+
)
1355+
assert collector.x == 10

0 commit comments

Comments
 (0)