-
- Notifications
You must be signed in to change notification settings - Fork 2.9k
More py.path -> pathlib conversions #8174
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
Changes from all commits
73586be
2c05a7b
042d12f
7aa2240
92ba96b
8b220fa
170a2c5
a218413
4faed28
ca4effc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The following changes have been made to internal pytest types/functions: | ||
| ||
- The ``path`` property of ``_pytest.code.Code`` returns ``Path`` instead of ``py.path.local``. | ||
- The ``path`` property of ``_pytest.code.TracebackEntry`` returns ``Path`` instead of ``py.path.local``. | ||
- The ``_pytest.code.getfslineno()`` function returns ``Path`` instead of ``py.path.local``. | ||
- The ``_pytest.python.path_matches_patterns()`` function takes ``Path`` instead of ``py.path.local``. |
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -43,6 +43,8 @@ | |
from _pytest._io.saferepr import saferepr | ||
from _pytest.compat import final | ||
from _pytest.compat import get_real_func | ||
from _pytest.pathlib import absolutepath | ||
from _pytest.pathlib import bestrelpath | ||
| ||
if TYPE_CHECKING: | ||
from typing_extensions import Literal | ||
| @@ -78,16 +80,16 @@ def name(self) -> str: | |
return self.raw.co_name | ||
| ||
@property | ||
def path(self) -> Union[py.path.local, str]: | ||
def path(self) -> Union[Path, str]: | ||
"""Return a path object pointing to source code, or an ``str`` in | ||
case of ``OSError`` / non-existing file.""" | ||
if not self.raw.co_filename: | ||
return "" | ||
try: | ||
p = py.path.local(self.raw.co_filename) | ||
p = absolutepath(self.raw.co_filename) | ||
# maybe don't try this checking | ||
if not p.check(): | ||
raise OSError("py.path check failed.") | ||
if not p.exists(): | ||
raise OSError("path check failed.") | ||
return p | ||
except OSError: | ||
# XXX maybe try harder like the weird logic | ||
| @@ -223,7 +225,7 @@ def statement(self) -> "Source": | |
return source.getstatement(self.lineno) | ||
| ||
@property | ||
def path(self) -> Union[py.path.local, str]: | ||
def path(self) -> Union[Path, str]: | ||
"""Path to the source code.""" | ||
return self.frame.code.path | ||
| ||
| @@ -336,10 +338,10 @@ def f(cur: TracebackType) -> Iterable[TracebackEntry]: | |
| ||
def cut( | ||
self, | ||
path=None, | ||
path: Optional[Union[Path, str]] = None, | ||
lineno: Optional[int] = None, | ||
firstlineno: Optional[int] = None, | ||
excludepath: Optional[py.path.local] = None, | ||
excludepath: Optional[Path] = None, | ||
) -> "Traceback": | ||
"""Return a Traceback instance wrapping part of this Traceback. | ||
| ||
| @@ -353,17 +355,19 @@ def cut( | |
for x in self: | ||
code = x.frame.code | ||
codepath = code.path | ||
if path is not None and codepath != path: | ||
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. 👍 to breaking the long conditional. | ||
continue | ||
if ( | ||
(path is None or codepath == path) | ||
and ( | ||
excludepath is None | ||
or not isinstance(codepath, py.path.local) | ||
or not codepath.relto(excludepath) | ||
) | ||
and (lineno is None or x.lineno == lineno) | ||
and (firstlineno is None or x.frame.code.firstlineno == firstlineno) | ||
excludepath is not None | ||
and isinstance(codepath, Path) | ||
and excludepath in codepath.parents | ||
): | ||
return Traceback(x._rawentry, self._excinfo) | ||
continue | ||
if lineno is not None and x.lineno != lineno: | ||
continue | ||
if firstlineno is not None and x.frame.code.firstlineno != firstlineno: | ||
continue | ||
return Traceback(x._rawentry, self._excinfo) | ||
return self | ||
| ||
@overload | ||
| @@ -801,7 +805,8 @@ def repr_traceback_entry( | |
message = "in %s" % (entry.name) | ||
else: | ||
message = excinfo and excinfo.typename or "" | ||
path = self._makepath(entry.path) | ||
entry_path = entry.path | ||
path = self._makepath(entry_path) | ||
reprfileloc = ReprFileLocation(path, entry.lineno + 1, message) | ||
localsrepr = self.repr_locals(entry.locals) | ||
return ReprEntry(lines, reprargs, localsrepr, reprfileloc, style) | ||
| @@ -814,15 +819,15 @@ def repr_traceback_entry( | |
lines.extend(self.get_exconly(excinfo, indent=4)) | ||
return ReprEntry(lines, None, None, None, style) | ||
| ||
def _makepath(self, path): | ||
if not self.abspath: | ||
def _makepath(self, path: Union[Path, str]) -> str: | ||
if not self.abspath and isinstance(path, Path): | ||
try: | ||
np = py.path.local().bestrelpath(path) | ||
np = bestrelpath(Path.cwd(), path) | ||
except OSError: | ||
return path | ||
return str(path) | ||
if len(np) < len(str(path)): | ||
path = np | ||
return path | ||
return np | ||
return str(path) | ||
| ||
def repr_traceback(self, excinfo: ExceptionInfo[BaseException]) -> "ReprTraceback": | ||
traceback = excinfo.traceback | ||
| @@ -1181,7 +1186,7 @@ def toterminal(self, tw: TerminalWriter) -> None: | |
tw.line("") | ||
| ||
| ||
def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]: | ||
def getfslineno(obj: object) -> Tuple[Union[str, Path], int]: | ||
"""Return source location (path, lineno) for the given object. | ||
| ||
If the source cannot be determined return ("", -1). | ||
| @@ -1203,7 +1208,7 @@ def getfslineno(obj: object) -> Tuple[Union[str, py.path.local], int]: | |
except TypeError: | ||
return "", -1 | ||
| ||
fspath = fn and py.path.local(fn) or "" | ||
fspath = fn and absolutepath(fn) or "" | ||
lineno = -1 | ||
if fspath: | ||
try: | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
| @@ -36,6 +36,7 @@ | |||||
from _pytest.fixtures import FixtureRequest | ||||||
from _pytest.nodes import Collector | ||||||
from _pytest.outcomes import OutcomeException | ||||||
from _pytest.pathlib import fnmatch_ex | ||||||
from _pytest.pathlib import import_path | ||||||
from _pytest.python_api import approx | ||||||
from _pytest.warning_types import PytestWarning | ||||||
| @@ -120,32 +121,32 @@ def pytest_unconfigure() -> None: | |||||
| ||||||
| ||||||
def pytest_collect_file( | ||||||
path: py.path.local, parent: Collector, | ||||||
fspath: Path, path: py.path.local, parent: Collector, | ||||||
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. We don't use Suggested change
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. It's actually still used, just well hidden with the | ||||||
) -> Optional[Union["DoctestModule", "DoctestTextfile"]]: | ||||||
config = parent.config | ||||||
if path.ext == ".py": | ||||||
if config.option.doctestmodules and not _is_setup_py(path): | ||||||
if fspath.suffix == ".py": | ||||||
if config.option.doctestmodules and not _is_setup_py(fspath): | ||||||
mod: DoctestModule = DoctestModule.from_parent(parent, fspath=path) | ||||||
return mod | ||||||
elif _is_doctest(config, path, parent): | ||||||
elif _is_doctest(config, fspath, parent): | ||||||
txt: DoctestTextfile = DoctestTextfile.from_parent(parent, fspath=path) | ||||||
return txt | ||||||
return None | ||||||
| ||||||
| ||||||
def _is_setup_py(path: py.path.local) -> bool: | ||||||
if path.basename != "setup.py": | ||||||
def _is_setup_py(path: Path) -> bool: | ||||||
if path.name != "setup.py": | ||||||
return False | ||||||
contents = path.read_binary() | ||||||
contents = path.read_bytes() | ||||||
return b"setuptools" in contents or b"distutils" in contents | ||||||
| ||||||
| ||||||
def _is_doctest(config: Config, path: py.path.local, parent) -> bool: | ||||||
if path.ext in (".txt", ".rst") and parent.session.isinitpath(path): | ||||||
def _is_doctest(config: Config, path: Path, parent: Collector) -> bool: | ||||||
if path.suffix in (".txt", ".rst") and parent.session.isinitpath(path): | ||||||
return True | ||||||
globs = config.getoption("doctestglob") or ["test*.txt"] | ||||||
for glob in globs: | ||||||
if path.check(fnmatch=glob): | ||||||
if fnmatch_ex(glob, path): | ||||||
return True | ||||||
return False | ||||||
| ||||||
|
Uh oh!
There was an error while loading. Please reload this page.