Skip to content
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

Fix 6341 disallow session config in fromparent #6387

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
4 changes: 4 additions & 0 deletions changelog/5975.deprecation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ Instead they are new constructed via ``Node.from_parent``.

This transitional mechanism enables us to detangle the very intensely
entangled ``Node`` relationships by enforcing more controlled creation/configruation patterns.

As part of that session/config are already disallowed parameters and as we work on the details we might need disallow a few more as well.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
As part of that session/config are already disallowed parameters and as we work on the details we might need disallow a few more as well.
As part of that session/config are already disallowed parameters and as we work on the details we might need to disallow a few more as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This grammar fix was not included.


Subclasses are expected to use `super().from_parent` if they intend to expand the creation of `Nodes`.
10 changes: 8 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,14 @@ def __init__(self, name, parent, runner=None, dtest=None):
self.fixture_request = None

@classmethod
def from_parent(cls, parent, *, name, runner, dtest):
return cls._create(name=name, parent=parent, runner=runner, dtest=dtest)
def from_parent( # type: ignore
cls, parent: "Union[DoctestTextfile, DoctestModule]", *, name, runner, dtest
):
# incompatible signature due to to imposed limits on sublcass
"""
the public named constructor
"""
return super().from_parent(name=name, parent=parent, runner=runner, dtest=dtest)

def setup(self):
if self.dtest is not None:
Expand Down
23 changes: 20 additions & 3 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,22 @@ def __init__(
self._nodeid += "::" + self.name

@classmethod
def from_parent(cls, parent, *, name):
return cls._create(parent=parent, name=name)
def from_parent(cls, parent: "Node", **kw):
"""
Public Constructor for Nodes

This indirection got introduced in order to enable removing
the fragile logic from the node constructors.

Subclasses can use ``super().from_parent(...)`` when overriding the construction

:param parent: the parent node of this test Node
"""
if "config" in kw:
raise TypeError("config is not a valid argument for from_parent")
if "session" in kw:
raise TypeError("session is not a valid argument for from_parent")
return cls._create(parent=parent, **kw)

@property
def ihook(self):
Expand Down Expand Up @@ -434,7 +448,10 @@ def __init__(

@classmethod
def from_parent(cls, parent, *, fspath):
return cls._create(parent=parent, fspath=fspath)
"""
The public constructor
"""
return super().from_parent(parent=parent, fspath=fspath)


class File(FSCollector):
Expand Down
12 changes: 9 additions & 3 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ class Class(PyCollector):

@classmethod
def from_parent(cls, parent, *, name, obj=None):
return cls._create(name=name, parent=parent)
"""
The public constructor
"""
return super().from_parent(name=name, parent=parent)

def collect(self):
if not safe_getattr(self.obj, "__test__", True):
Expand Down Expand Up @@ -1458,8 +1461,11 @@ def __init__(
self.originalname = originalname

@classmethod
def from_parent(cls, parent, **kw):
return cls._create(parent=parent, **kw)
def from_parent(cls, parent, **kw): # todo: determine sound type limitations
"""
The public constructor
"""
return super().from_parent(parent=parent, **kw)

def _initrequest(self):
self.funcargs = {}
Expand Down
2 changes: 1 addition & 1 deletion testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def make_function(testdir, **kwargs):
session = testdir.Session.from_config(config)
session._fixturemanager = FixtureManager(session)

return pytest.Function.from_parent(config=config, parent=session, **kwargs)
return pytest.Function.from_parent(parent=session, **kwargs)

def test_function_equality(self, testdir, tmpdir):
def func1():
Expand Down
7 changes: 7 additions & 0 deletions testing/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def test_ischildnode(baseid, nodeid, expected):
assert result is expected


def test_node_from_parent_disallowed_arguments():
with pytest.raises(TypeError, match="session is"):
nodes.Node.from_parent(None, session=None)
with pytest.raises(TypeError, match="config is"):
nodes.Node.from_parent(None, config=None)


def test_std_warn_not_pytestwarning(testdir):
items = testdir.getitems(
"""
Expand Down