From 378af02cd9b7ed912e5e644d7034a086b5dac5ec Mon Sep 17 00:00:00 2001 From: OMOTO Tsukasa Date: Sun, 13 Mar 2022 14:10:10 +0900 Subject: [PATCH] Replace `fspath` argument for Node constructors with `pathlib.Path` (#82) https://docs.pytest.org/en/latest/deprecations.html#fspath-argument-for-node-constructors-replaced-with-pathlib-path ``` ../../../../../../../../../Users/henry/.pyenv/versions/3.9.10/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_pytest/nodes.py:140 /Users/henry/.pyenv/versions/3.9.10/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/_pytest/nodes.py:140: PytestRemovedIn8Warning: The (fspath: py.path.local) argument to File is deprecated. Please use the (path: pathlib.Path) argument instead. See https://docs.pytest.org/en/latest/deprecations.html#fspath-argument-for-node-constructors-replaced-with-pathlib-path return super().__call__(*k, **kw) ``` --- src/pytest_pydocstyle.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pytest_pydocstyle.py b/src/pytest_pydocstyle.py index 58701cf..c0e5485 100644 --- a/src/pytest_pydocstyle.py +++ b/src/pytest_pydocstyle.py @@ -3,6 +3,7 @@ import contextlib import logging +import pathlib import sys import pydocstyle @@ -34,29 +35,34 @@ def _patch_sys_argv(arguments): sys.argv = old_args -def pytest_collect_file(parent, path): +def pytest_collect_file(file_path: pathlib.Path, path, parent): + """Create a Collector for the given path, or None if not relevant. + + See: + - https://docs.pytest.org/en/7.0.x/reference/reference.html#pytest.hookspec.pytest_collect_file + """ config = parent.config - if config.getoption('pydocstyle') and path.ext == '.py': + if config.getoption('pydocstyle') and file_path.suffix == '.py': parser = pydocstyle.config.ConfigurationParser() - args = [str(path.basename)] + args = [file_path.name] with _patch_sys_argv(args): parser.parse() for filename, _, _ in parser.get_files_to_check(): - # https://github.com/pytest-dev/pytest/blob/ee1950af7793624793ee297e5f48b49c8bdf2065/src/_pytest/nodes.py#L477 - return File.from_parent(parent=parent, fspath=path, config_parser=parser) + return File.from_parent(parent=parent, path=file_path, config_parser=parser) class File(pytest.File): @classmethod - def from_parent(cls, parent, fspath, config_parser: pydocstyle.config.ConfigurationParser): - _file = super().from_parent(parent=parent, fspath=fspath) + def from_parent(cls, parent, path: pathlib.Path, config_parser: pydocstyle.config.ConfigurationParser): + # https://github.com/pytest-dev/pytest/blob/3e4c14bfaa046bcb5b75903470accf83d93f01ce/src/_pytest/nodes.py#L624 + _file = super().from_parent(parent=parent, path=path) # store config parser of pydocstyle _file.config_parser = config_parser return _file def collect(self): - # https://github.com/pytest-dev/pytest/blob/ee1950af7793624793ee297e5f48b49c8bdf2065/src/_pytest/nodes.py#L399 + # https://github.com/pytest-dev/pytest/blob/3e4c14bfaa046bcb5b75903470accf83d93f01ce/src/_pytest/nodes.py#L524 yield Item.from_parent(parent=self, name=self.name, nodeid=self.nodeid)