From 54a154c86f4806327081b80193cebca7934468d0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 23 Feb 2021 17:56:42 +0100 Subject: [PATCH 1/2] Allow Class.from_parent to forward custom parameters to the constructor Similarly to #7143, at work we have a project with a custom pytest.Class subclass, adding an additional argument to the constructor. All from_parent implementations in pytest accept and forward *kw, except Class (before this change) and DoctestItem - since I'm not familiar with doctest support, I've left the latter as-is. --- changelog/8367.bugfix.rst | 1 + src/_pytest/python.py | 4 ++-- testing/test_collection.py | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelog/8367.bugfix.rst diff --git a/changelog/8367.bugfix.rst b/changelog/8367.bugfix.rst new file mode 100644 index 000000000..f4b036701 --- /dev/null +++ b/changelog/8367.bugfix.rst @@ -0,0 +1 @@ +Fix ``Class.from_parent`` so it forwards extra keyword arguments to the constructor. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 726241cb5..944c395a8 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -763,9 +763,9 @@ class Class(PyCollector): """Collector for test methods.""" @classmethod - def from_parent(cls, parent, *, name, obj=None): + def from_parent(cls, parent, *, name, obj=None, **kw): """The public constructor.""" - return super().from_parent(name=name, parent=parent) + return super().from_parent(name=name, parent=parent, **kw) def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: if not safe_getattr(self.obj, "__test__", True): diff --git a/testing/test_collection.py b/testing/test_collection.py index 3dd9283ec..39538ae98 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1360,6 +1360,24 @@ def test_fscollector_from_parent(pytester: Pytester, request: FixtureRequest) -> assert collector.x == 10 +def test_class_from_parent(pytester: Pytester, request: FixtureRequest) -> None: + """Ensure Class.from_parent can forward custom arguments to the constructor.""" + + class MyCollector(pytest.Class): + def __init__(self, name, parent, x): + super().__init__(name, parent) + self.x = x + + @classmethod + def from_parent(cls, parent, *, name, x): + return super().from_parent(parent=parent, name=name, x=x) + + collector = MyCollector.from_parent( + parent=request.session, name="foo", x=10 + ) + assert collector.x == 10 + + class TestImportModeImportlib: def test_collect_duplicate_names(self, pytester: Pytester) -> None: """--import-mode=importlib can import modules with same names that are not in packages.""" From 3b7fc2c9c839148d19518af655a1d347351286b0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Feb 2021 17:02:45 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- testing/test_collection.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testing/test_collection.py b/testing/test_collection.py index 39538ae98..298c2dde1 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -1372,9 +1372,7 @@ def test_class_from_parent(pytester: Pytester, request: FixtureRequest) -> None: def from_parent(cls, parent, *, name, x): return super().from_parent(parent=parent, name=name, x=x) - collector = MyCollector.from_parent( - parent=request.session, name="foo", x=10 - ) + collector = MyCollector.from_parent(parent=request.session, name="foo", x=10) assert collector.x == 10