Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Replace fspath argument for Node constructors with pathlib.Path (#82
Browse files Browse the repository at this point in the history
)

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)
```
  • Loading branch information
henry0312 authored Mar 13, 2022
1 parent a821441 commit 378af02
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/pytest_pydocstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import contextlib
import logging
import pathlib
import sys

import pydocstyle
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 378af02

Please sign in to comment.