Skip to content

Commit fcef7e4

Browse files
authored
Merge pull request pytest-dev#9447 from bluetech/code-cut-pathlike
code: accept any `os.PathLike[str]` in `Traceback.cut`
2 parents 55debfa + 0da4760 commit fcef7e4

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/_pytest/_code/code.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ast
22
import inspect
3+
import os
34
import re
45
import sys
56
import traceback
@@ -343,10 +344,10 @@ def f(cur: TracebackType) -> Iterable[TracebackEntry]:
343344

344345
def cut(
345346
self,
346-
path: Optional[Union[Path, str]] = None,
347+
path: Optional[Union["os.PathLike[str]", str]] = None,
347348
lineno: Optional[int] = None,
348349
firstlineno: Optional[int] = None,
349-
excludepath: Optional[Path] = None,
350+
excludepath: Optional["os.PathLike[str]"] = None,
350351
) -> "Traceback":
351352
"""Return a Traceback instance wrapping part of this Traceback.
352353
@@ -357,15 +358,17 @@ def cut(
357358
for formatting reasons (removing some uninteresting bits that deal
358359
with handling of the exception/traceback).
359360
"""
361+
path_ = None if path is None else os.fspath(path)
362+
excludepath_ = None if excludepath is None else os.fspath(excludepath)
360363
for x in self:
361364
code = x.frame.code
362365
codepath = code.path
363-
if path is not None and codepath != path:
366+
if path is not None and str(codepath) != path_:
364367
continue
365368
if (
366369
excludepath is not None
367370
and isinstance(codepath, Path)
368-
and excludepath in codepath.parents
371+
and excludepath_ in (str(p) for p in codepath.parents) # type: ignore[operator]
369372
):
370373
continue
371374
if lineno is not None and x.lineno != lineno:

0 commit comments

Comments
 (0)