Skip to content

Commit 123f71f

Browse files
committed
Raise AttributeError for File.gethookproxy
1 parent 122b10c commit 123f71f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/_pytest/nodes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ def __init__(
432432
self._norecursepatterns = self.config.getini("norecursedirs")
433433

434434
def gethookproxy(self, fspath):
435+
from _pytest.main import Session # noqa: F811
436+
from _pytest.python import Package
437+
438+
if not isinstance(self, (Session, Package)):
439+
# Prevent making it "more public" with the move to the base class.
440+
raise AttributeError()
441+
435442
# check if we have the common case of running
436443
# hooks with all conftest.py files
437444
pm = self.config.pluginmanager

testing/test_nodes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,13 @@ class FakeSession:
5151

5252
outside = py.path.local("/outside")
5353
assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None
54+
55+
56+
def test_node_gethookproxy_only_on_session_and_package(pytestconfig):
57+
file = nodes.File(
58+
fspath=py.path.local(), session=object, nodeid="nodeid", config=pytestconfig
59+
)
60+
with pytest.raises(TypeError):
61+
file.gethookproxy()
62+
with pytest.raises(AttributeError):
63+
file.gethookproxy("foo")

0 commit comments

Comments
 (0)