-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
address #8361 - introduce hook caller wrappers that enable backward compat #8463
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
Merged
RonnyPfannschmidt
merged 9 commits into
pytest-dev:main
from
RonnyPfannschmidt:workaround-8361
Apr 5, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
30f1b81
address #8361 - introduce hook caller wrappers that enable backward c…
RonnyPfannschmidt a550db4
drop internal py.path.local objects from hook calls
RonnyPfannschmidt 2994022
reshape typing for hook invocation proxying
RonnyPfannschmidt 4ddf6c6
test warnings and fix invocation bugs
RonnyPfannschmidt 7ac7610
add tresting for implication
RonnyPfannschmidt bad1963
fix #8361: address review/quality comments
RonnyPfannschmidt deb5b5b
fix #8371: fix stack-level
RonnyPfannschmidt 53ebe34
Apply suggestions from code review
RonnyPfannschmidt aa10bff
fix deprecation test for path/fspath hook args
RonnyPfannschmidt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import functools | ||
import warnings | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from ..compat import LEGACY_PATH | ||
from ..deprecated import HOOK_LEGACY_PATH_ARG | ||
from _pytest.nodes import _imply_path | ||
|
||
# hookname: (Path, LEGACY_PATH) | ||
imply_paths_hooks = { | ||
"pytest_ignore_collect": ("fspath", "path"), | ||
"pytest_collect_file": ("fspath", "path"), | ||
"pytest_pycollect_makemodule": ("fspath", "path"), | ||
"pytest_report_header": ("startpath", "startdir"), | ||
"pytest_report_collectionfinish": ("startpath", "startdir"), | ||
} | ||
|
||
|
||
class PathAwareHookProxy: | ||
""" | ||
this helper wraps around hook callers | ||
until pluggy supports fixingcalls, this one will do | ||
|
||
it currently doesnt return full hook caller proxies for fixed hooks, | ||
this may have to be changed later depending on bugs | ||
""" | ||
|
||
def __init__(self, hook_caller): | ||
self.__hook_caller = hook_caller | ||
|
||
def __dir__(self): | ||
RonnyPfannschmidt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return dir(self.__hook_caller) | ||
|
||
def __getattr__(self, key, _wraps=functools.wraps): | ||
hook = getattr(self.__hook_caller, key) | ||
if key not in imply_paths_hooks: | ||
self.__dict__[key] = hook | ||
return hook | ||
else: | ||
path_var, fspath_var = imply_paths_hooks[key] | ||
|
||
@_wraps(hook) | ||
def fixed_hook(**kw): | ||
RonnyPfannschmidt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
path_value: Optional[Path] = kw.pop(path_var, None) | ||
fspath_value: Optional[LEGACY_PATH] = kw.pop(fspath_var, None) | ||
if fspath_value is not None: | ||
warnings.warn( | ||
HOOK_LEGACY_PATH_ARG.format( | ||
pylib_path_arg=fspath_var, pathlib_path_arg=path_var | ||
RonnyPfannschmidt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
stacklevel=2, | ||
) | ||
path_value, fspath_value = _imply_path(path_value, fspath_value) | ||
kw[path_var] = path_value | ||
kw[fspath_var] = fspath_value | ||
return hook(**kw) | ||
|
||
fixed_hook.__name__ = key | ||
self.__dict__[key] = fixed_hook | ||
return fixed_hook | ||
RonnyPfannschmidt marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -551,7 +551,9 @@ def gethookproxy(self, fspath: "os.PathLike[str]"): | |
remove_mods = pm._conftest_plugins.difference(my_conftestmodules) | ||
if remove_mods: | ||
# One or more conftests are not in use at this fspath. | ||
proxy = FSHookProxy(pm, remove_mods) | ||
from .config.compat import PathAwareHookProxy | ||
|
||
proxy = PathAwareHookProxy(FSHookProxy(pm, remove_mods)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bluetech here i wrap that one |
||
else: | ||
# All plugins are active for this fspath. | ||
proxy = self.config.hook | ||
|
@@ -561,9 +563,8 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool: | |
if direntry.name == "__pycache__": | ||
return False | ||
fspath = Path(direntry.path) | ||
path = legacy_path(fspath) | ||
ihook = self.gethookproxy(fspath.parent) | ||
if ihook.pytest_ignore_collect(fspath=fspath, path=path, config=self.config): | ||
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): | ||
return False | ||
norecursepatterns = self.config.getini("norecursedirs") | ||
if any(fnmatch_ex(pat, fspath) for pat in norecursepatterns): | ||
|
@@ -573,17 +574,14 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool: | |
def _collectfile( | ||
self, fspath: Path, handle_dupes: bool = True | ||
) -> Sequence[nodes.Collector]: | ||
path = legacy_path(fspath) | ||
assert ( | ||
fspath.is_file() | ||
), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format( | ||
fspath, fspath.is_dir(), fspath.exists(), fspath.is_symlink() | ||
) | ||
ihook = self.gethookproxy(fspath) | ||
if not self.isinitpath(fspath): | ||
if ihook.pytest_ignore_collect( | ||
fspath=fspath, path=path, config=self.config | ||
): | ||
if ihook.pytest_ignore_collect(fspath=fspath, config=self.config): | ||
return () | ||
|
||
if handle_dupes: | ||
|
@@ -595,7 +593,7 @@ def _collectfile( | |
else: | ||
duplicate_paths.add(fspath) | ||
|
||
return ihook.pytest_collect_file(fspath=fspath, path=path, parent=self) # type: ignore[no-any-return] | ||
return ihook.pytest_collect_file(fspath=fspath, parent=self) # type: ignore[no-any-return] | ||
|
||
@overload | ||
def perform_collect( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.