Skip to content

Commit

Permalink
drop usage of py.path.local calls
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
RonnyPfannschmidt and nicoddemus committed Mar 6, 2021
1 parent 22dad53 commit 77cb110
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 131 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ repos:
xml\.
)
types: [python]
- id: py-path-deprecated
name: py.path usage is deprecated
language: pygrep
entry: \bpy\.path\.local
exclude: docs
types: [python]
3 changes: 1 addition & 2 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import attr
import pluggy
import py

import _pytest
from _pytest._code.source import findsource
Expand Down Expand Up @@ -1230,7 +1229,7 @@ def getfslineno(obj: object) -> Tuple[Union[str, Path], int]:
if _PLUGGY_DIR.name == "__init__.py":
_PLUGGY_DIR = _PLUGGY_DIR.parent
_PYTEST_DIR = Path(_pytest.__file__).parent
_PY_DIR = Path(py.__file__).parent
_PY_DIR = Path(__import__("py").__file__).parent


def filter_traceback(entry: TracebackEntry) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
from typing import Union

import attr
import py

from .pathlib import resolve_from_str
from .pathlib import rm_rf
from .reports import CollectReport
from _pytest import nodes
from _pytest._io import TerminalWriter
from _pytest.compat import final
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.config import Config
from _pytest.config import ExitCode
from _pytest.config import hookimpl
Expand Down Expand Up @@ -120,7 +121,7 @@ def warn(self, fmt: str, *, _ispytest: bool = False, **args: object) -> None:
stacklevel=3,
)

def makedir(self, name: str) -> py.path.local:
def makedir(self, name: str) -> LEGACY_PATH:
"""Return a directory path object with the given name.
If the directory does not yet exist, it will be created. You can use
Expand All @@ -137,7 +138,7 @@ def makedir(self, name: str) -> py.path.local:
raise ValueError("name is not allowed to contain path separators")
res = self._cachedir.joinpath(self._CACHE_PREFIX_DIRS, path)
res.mkdir(exist_ok=True, parents=True)
return py.path.local(res)
return legacy_path(res)

def _getvaluepath(self, key: str) -> Path:
return self._cachedir.joinpath(self._CACHE_PREFIX_VALUES, Path(key))
Expand Down
11 changes: 7 additions & 4 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
_T = TypeVar("_T")
_S = TypeVar("_S")

#: constant to prepare valuing py.path.local replacements/lazy proxies later on
#: constant to prepare valuing pylib path replacements/lazy proxies later on
# intended for removal in pytest 8.0 or 9.0

LEGACY_PATH = py.path.local
# fmt: off
# intentional space to create a fake difference for the verification
LEGACY_PATH = py.path. local
# fmt: on


def legacy_path(path: Union[str, "os.PathLike[str]"]) -> LEGACY_PATH:
"""Internal wrapper to prepare lazy proxies for py.path.local instances"""
return py.path.local(path)
"""Internal wrapper to prepare lazy proxies for legacy_path instances"""
return LEGACY_PATH(path)


# fmt: off
Expand Down
25 changes: 13 additions & 12 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from typing import Union

import attr
import py
from pluggy import HookimplMarker
from pluggy import HookspecMarker
from pluggy import PluginManager
Expand All @@ -48,6 +47,8 @@
from _pytest._io import TerminalWriter
from _pytest.compat import final
from _pytest.compat import importlib_metadata
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.outcomes import fail
from _pytest.outcomes import Skipped
from _pytest.pathlib import absolutepath
Expand Down Expand Up @@ -937,15 +938,15 @@ def __init__(
self.cache: Optional[Cache] = None

@property
def invocation_dir(self) -> py.path.local:
def invocation_dir(self) -> LEGACY_PATH:
"""The directory from which pytest was invoked.
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
which is a :class:`pathlib.Path`.
:type: py.path.local
:type: LEGACY_PATH
"""
return py.path.local(str(self.invocation_params.dir))
return legacy_path(str(self.invocation_params.dir))

@property
def rootpath(self) -> Path:
Expand All @@ -958,14 +959,14 @@ def rootpath(self) -> Path:
return self._rootpath

@property
def rootdir(self) -> py.path.local:
def rootdir(self) -> LEGACY_PATH:
"""The path to the :ref:`rootdir <rootdir>`.
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
:type: py.path.local
:type: LEGACY_PATH
"""
return py.path.local(str(self.rootpath))
return legacy_path(str(self.rootpath))

@property
def inipath(self) -> Optional[Path]:
Expand All @@ -978,14 +979,14 @@ def inipath(self) -> Optional[Path]:
return self._inipath

@property
def inifile(self) -> Optional[py.path.local]:
def inifile(self) -> Optional[LEGACY_PATH]:
"""The path to the :ref:`configfile <configfiles>`.
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
:type: Optional[py.path.local]
:type: Optional[LEGACY_PATH]
"""
return py.path.local(str(self.inipath)) if self.inipath else None
return legacy_path(str(self.inipath)) if self.inipath else None

def add_cleanup(self, func: Callable[[], None]) -> None:
"""Add a function to be called when the config object gets out of
Expand Down Expand Up @@ -1420,7 +1421,7 @@ def _getini(self, name: str):
assert self.inipath is not None
dp = self.inipath.parent
input_values = shlex.split(value) if isinstance(value, str) else value
return [py.path.local(str(dp / x)) for x in input_values]
return [legacy_path(str(dp / x)) for x in input_values]
elif type == "args":
return shlex.split(value) if isinstance(value, str) else value
elif type == "linelist":
Expand All @@ -1446,7 +1447,7 @@ def _getconftest_pathlist(self, name: str, path: Path) -> Optional[List[Path]]:
for relroot in relroots:
if isinstance(relroot, Path):
pass
elif isinstance(relroot, py.path.local):
elif isinstance(relroot, LEGACY_PATH):
relroot = Path(relroot)
else:
relroot = relroot.replace("/", os.sep)
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
NODE_FSPATH = UnformattedWarning(
PytestDeprecationWarning,
"{type}.fspath is deprecated and will be replaced by {type}.path.\n"
"see TODO;URL for details on replacing py.path.local with pathlib.Path",
"see https://docs.pytest.org/en/latest/deprecations.html#node-fspath-in-favor-of-pathlib-and-node-path",
)

# You want to make some `__init__` or function "private".
Expand Down
5 changes: 2 additions & 3 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
from typing import TYPE_CHECKING
from typing import Union

import py.path

import pytest
from _pytest import outcomes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
from _pytest._code.code import TerminalRepr
from _pytest._io import TerminalWriter
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.compat import safe_getattr
from _pytest.config import Config
Expand Down Expand Up @@ -123,7 +122,7 @@ def pytest_unconfigure() -> None:

def pytest_collect_file(
fspath: Path,
path: py.path.local,
path: LEGACY_PATH,
parent: Collector,
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]:
config = parent.config
Expand Down
22 changes: 11 additions & 11 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import TYPE_CHECKING
from typing import Union

import py.path
from pluggy import HookspecMarker

from _pytest.deprecated import WARNING_CAPTURED_HOOK
Expand Down Expand Up @@ -42,6 +41,7 @@
from _pytest.reports import TestReport
from _pytest.runner import CallInfo
from _pytest.terminal import TerminalReporter
from _pytest.compat import LEGACY_PATH


hookspec = HookspecMarker("pytest")
Expand Down Expand Up @@ -263,7 +263,7 @@ def pytest_collection_finish(session: "Session") -> None:

@hookspec(firstresult=True)
def pytest_ignore_collect(
fspath: Path, path: py.path.local, config: "Config"
fspath: Path, path: "LEGACY_PATH", config: "Config"
) -> Optional[bool]:
"""Return True to prevent considering this path for collection.
Expand All @@ -273,7 +273,7 @@ def pytest_ignore_collect(
Stops at first non-None result, see :ref:`firstresult`.
:param pathlib.Path fspath: The path to analyze.
:param py.path.local path: The path to analyze.
:param LEGACY_PATH path: The path to analyze.
:param _pytest.config.Config config: The pytest config object.
.. versionchanged:: 6.3.0
Expand All @@ -283,14 +283,14 @@ def pytest_ignore_collect(


def pytest_collect_file(
fspath: Path, path: py.path.local, parent: "Collector"
fspath: Path, path: "LEGACY_PATH", parent: "Collector"
) -> "Optional[Collector]":
"""Create a Collector for the given path, or None if not relevant.
The new node needs to have the specified ``parent`` as a parent.
:param pathlib.Path fspath: The path to analyze.
:param py.path.local path: The path to collect.
:param LEGACY_PATH path: The path to collect.
.. versionchanged:: 6.3.0
The ``fspath`` parameter was added as a :class:`pathlib.Path`
Expand Down Expand Up @@ -335,7 +335,7 @@ def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectRepor

@hookspec(firstresult=True)
def pytest_pycollect_makemodule(
fspath: Path, path: py.path.local, parent
fspath: Path, path: "LEGACY_PATH", parent
) -> Optional["Module"]:
"""Return a Module collector or None for the given path.
Expand All @@ -346,7 +346,7 @@ def pytest_pycollect_makemodule(
Stops at first non-None result, see :ref:`firstresult`.
:param pathlib.Path fspath: The path of the module to collect.
:param py.path.local path: The path of the module to collect.
:param legacy_path path: The path of the module to collect.
.. versionchanged:: 6.3.0
The ``fspath`` parameter was added as a :class:`pathlib.Path`
Expand Down Expand Up @@ -676,13 +676,13 @@ def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> No


def pytest_report_header(
config: "Config", startpath: Path, startdir: py.path.local
config: "Config", startpath: Path, startdir: "LEGACY_PATH"
) -> Union[str, List[str]]:
"""Return a string or list of strings to be displayed as header info for terminal reporting.
:param _pytest.config.Config config: The pytest config object.
:param Path startpath: The starting dir.
:param py.path.local startdir: The starting dir.
:param LEGACY_PATH startdir: The starting dir.
.. note::
Expand All @@ -706,7 +706,7 @@ def pytest_report_header(
def pytest_report_collectionfinish(
config: "Config",
startpath: Path,
startdir: py.path.local,
startdir: "LEGACY_PATH",
items: Sequence["Item"],
) -> Union[str, List[str]]:
"""Return a string or list of strings to be displayed after collection
Expand All @@ -718,7 +718,7 @@ def pytest_report_collectionfinish(
:param _pytest.config.Config config: The pytest config object.
:param Path startpath: The starting path.
:param py.path.local startdir: The starting dir.
:param LEGACY_PATH startdir: The starting dir.
:param items: List of pytest items that are going to be executed; this list should not be modified.
.. note::
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from typing import Union

import attr
import py

import _pytest._code
from _pytest import nodes
from _pytest.compat import final
from _pytest.compat import legacy_path
from _pytest.config import Config
from _pytest.config import directory_arg
from _pytest.config import ExitCode
Expand Down Expand Up @@ -543,7 +543,7 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool:
if direntry.name == "__pycache__":
return False
fspath = Path(direntry.path)
path = py.path.local(fspath)
path = legacy_path(fspath)
ihook = self.gethookproxy(fspath.parent)
if ihook.pytest_ignore_collect(fspath=fspath, path=path, config=self.config):
return False
Expand All @@ -555,7 +555,7 @@ def _recurse(self, direntry: "os.DirEntry[str]") -> bool:
def _collectfile(
self, fspath: Path, handle_dupes: bool = True
) -> Sequence[nodes.Collector]:
path = py.path.local(fspath)
path = legacy_path(fspath)
assert (
fspath.is_file()
), "{!r} is not a file (isdir={!r}, exists={!r}, islink={!r})".format(
Expand Down
Loading

0 comments on commit 77cb110

Please sign in to comment.