Merge pull request #9184 from bluetech/reportinfo-pathlike

[7.0] Change `Node.reportinfo()` return value from `py.path` to `str|os.PathLike[str]`
This commit is contained in:
Ran Benita
2021-10-11 14:33:03 +03:00
committed by GitHub
8 changed files with 28 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
"""Discover and run doctests in modules and test files."""
import bdb
import inspect
import os
import platform
import sys
import traceback
@@ -28,7 +29,6 @@ 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 safe_getattr
from _pytest.config import Config
from _pytest.config.argparsing import Parser
@@ -371,9 +371,9 @@ class DoctestItem(pytest.Item):
reprlocation_lines.append((reprlocation, lines))
return ReprFailDoctest(reprlocation_lines)
def reportinfo(self):
def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
assert self.dtest is not None
return legacy_path(self.path), self.dtest.lineno, "[doctest] %s" % self.name
return self.path, self.dtest.lineno, "[doctest] %s" % self.name
def _get_flag_lookup() -> Dict[str, int]:

View File

@@ -718,15 +718,13 @@ class Item(Node):
if content:
self._report_sections.append((when, key, content))
def reportinfo(self) -> Tuple[Union[LEGACY_PATH, str], Optional[int], str]:
# TODO: enable Path objects in reportinfo
return legacy_path(self.path), None, ""
def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
return self.path, None, ""
@cached_property
def location(self) -> Tuple[str, Optional[int], str]:
location = self.reportinfo()
fspath = absolutepath(str(location[0]))
relfspath = self.session._node_location_to_relpath(fspath)
path = absolutepath(os.fspath(location[0]))
relfspath = self.session._node_location_to_relpath(path)
assert type(location[2]) is str
return (relfspath, location[1], location[2])

View File

@@ -48,7 +48,6 @@ from _pytest.compat import getlocation
from _pytest.compat import is_async_function
from _pytest.compat import is_generator
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.compat import NOTSET
from _pytest.compat import safe_getattr
from _pytest.compat import safe_isclass
@@ -321,7 +320,7 @@ class PyobjMixin(nodes.Node):
parts.reverse()
return ".".join(parts)
def reportinfo(self) -> Tuple[Union[LEGACY_PATH, str], int, str]:
def reportinfo(self) -> Tuple[Union["os.PathLike[str]", str], Optional[int], str]:
# XXX caching?
obj = self.obj
compat_co_firstlineno = getattr(obj, "compat_co_firstlineno", None)
@@ -330,17 +329,13 @@ class PyobjMixin(nodes.Node):
file_path = sys.modules[obj.__module__].__file__
if file_path.endswith(".pyc"):
file_path = file_path[:-1]
fspath: Union[LEGACY_PATH, str] = file_path
path: Union["os.PathLike[str]", str] = file_path
lineno = compat_co_firstlineno
else:
path, lineno = getfslineno(obj)
if isinstance(path, Path):
fspath = legacy_path(path)
else:
fspath = path
modpath = self.getmodpath()
assert isinstance(lineno, int)
return fspath, lineno, modpath
return path, lineno, modpath
# As an optimization, these builtin attribute names are pre-ignored when

View File

@@ -324,9 +324,9 @@ class TestReport(BaseReport):
outcome = "skipped"
r = excinfo._getreprcrash()
if excinfo.value._use_item_location:
filename, line = item.reportinfo()[:2]
path, line = item.reportinfo()[:2]
assert line is not None
longrepr = str(filename), line + 1, r.message
longrepr = os.fspath(path), line + 1, r.message
else:
longrepr = (str(r.path), r.lineno, r.message)
else: