Skip to content
Open
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/14728.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved the type annotations of ``Node.ihook`` and ``Session.gethookproxy``.
1 change: 1 addition & 0 deletions doc/en/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
("py:class", "_pytest.logging.LogCaptureHandler"),
("py:class", "_pytest.mark.structures.ParameterSet"),
# Intentionally undocumented/private
("py:class", "FSHookProxy"),
("py:class", "_pytest._code.code.Traceback"),
("py:class", "_pytest._py.path.LocalPath"),
("py:class", "_pytest.capture.CaptureResult"),
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,18 +728,18 @@ def isinitpath(
else:
return path_ in self._initialpaths

def gethookproxy(self, fspath: os.PathLike[str]) -> pluggy.HookRelay:
def gethookproxy(self, fspath: os.PathLike[str]) -> pluggy.HookRelay | FSHookProxy:
# Optimization: Path(Path(...)) is much slower than isinstance.
path = fspath if isinstance(fspath, Path) else Path(fspath)
pm = self.config.pluginmanager
# Check if we have the common case of running
# hooks with all conftest.py files.
my_conftestmodules = pm._getconftestmodules(path)
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
proxy: pluggy.HookRelay
proxy: pluggy.HookRelay | FSHookProxy
if remove_mods:
# One or more conftests are not in use at this path.
proxy = FSHookProxy(pm, remove_mods) # type: ignore[assignment]
proxy = FSHookProxy(pm, remove_mods)
else:
# All plugins are active for this fspath.
proxy = self.config.hook
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from typing_extensions import Self

# Imported here due to circular import.
from _pytest.main import FSHookProxy
from _pytest.main import Session


Expand Down Expand Up @@ -225,7 +226,7 @@ def from_parent(cls, parent: Node, **kw) -> Self:
return cls._create(parent=parent, **kw)

@property
def ihook(self) -> pluggy.HookRelay:
def ihook(self) -> pluggy.HookRelay | FSHookProxy:
"""Path-sensitive hook proxy used to call pytest hooks."""
return self.session.gethookproxy(self.path)

Expand Down