Skip to content

Commit 2681b0a

Browse files
committed
typing: pytester: LineMatcher
1 parent b10ab02 commit 2681b0a

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

src/_pytest/pytester.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ class RunResult:
413413
def __init__(
414414
self,
415415
ret: Union[int, ExitCode],
416-
outlines: Sequence[str],
417-
errlines: Sequence[str],
416+
outlines: List[str],
417+
errlines: List[str],
418418
duration: float,
419419
) -> None:
420420
try:
@@ -1327,48 +1327,42 @@ class LineMatcher:
13271327
13281328
The constructor takes a list of lines without their trailing newlines, i.e.
13291329
``text.splitlines()``.
1330-
13311330
"""
13321331

1333-
def __init__(self, lines):
1332+
def __init__(self, lines: List[str]) -> None:
13341333
self.lines = lines
1335-
self._log_output = []
1336-
1337-
def str(self):
1338-
"""Return the entire original text."""
1339-
return "\n".join(self.lines)
1334+
self._log_output = [] # type: List[str]
13401335

1341-
def _getlines(self, lines2):
1336+
def _getlines(self, lines2: Union[str, Sequence[str], Source]) -> Sequence[str]:
13421337
if isinstance(lines2, str):
13431338
lines2 = Source(lines2)
13441339
if isinstance(lines2, Source):
13451340
lines2 = lines2.strip().lines
13461341
return lines2
13471342

1348-
def fnmatch_lines_random(self, lines2):
1343+
def fnmatch_lines_random(self, lines2: Sequence[str]) -> None:
13491344
"""Check lines exist in the output using in any order.
13501345
13511346
Lines are checked using ``fnmatch.fnmatch``. The argument is a list of
13521347
lines which have to occur in the output, in any order.
1353-
13541348
"""
13551349
self._match_lines_random(lines2, fnmatch)
13561350

1357-
def re_match_lines_random(self, lines2):
1351+
def re_match_lines_random(self, lines2: Sequence[str]) -> None:
13581352
"""Check lines exist in the output using ``re.match``, in any order.
13591353
13601354
The argument is a list of lines which have to occur in the output, in
13611355
any order.
1362-
13631356
"""
1364-
self._match_lines_random(lines2, lambda name, pat: re.match(pat, name))
1357+
self._match_lines_random(lines2, lambda name, pat: bool(re.match(pat, name)))
13651358

1366-
def _match_lines_random(self, lines2, match_func):
1359+
def _match_lines_random(
1360+
self, lines2: Sequence[str], match_func: Callable[[str, str], bool]
1361+
) -> None:
13671362
"""Check lines exist in the output.
13681363
13691364
The argument is a list of lines which have to occur in the output, in
13701365
any order. Each line can contain glob whildcards.
1371-
13721366
"""
13731367
lines2 = self._getlines(lines2)
13741368
for line in lines2:
@@ -1380,25 +1374,24 @@ def _match_lines_random(self, lines2, match_func):
13801374
self._log("line %r not found in output" % line)
13811375
raise ValueError(self._log_text)
13821376

1383-
def get_lines_after(self, fnline):
1377+
def get_lines_after(self, fnline: str) -> Sequence[str]:
13841378
"""Return all lines following the given line in the text.
13851379
13861380
The given line can contain glob wildcards.
1387-
13881381
"""
13891382
for i, line in enumerate(self.lines):
13901383
if fnline == line or fnmatch(line, fnline):
13911384
return self.lines[i + 1 :]
13921385
raise ValueError("line %r not found in output" % fnline)
13931386

1394-
def _log(self, *args):
1387+
def _log(self, *args) -> None:
13951388
self._log_output.append(" ".join(str(x) for x in args))
13961389

13971390
@property
1398-
def _log_text(self):
1391+
def _log_text(self) -> str:
13991392
return "\n".join(self._log_output)
14001393

1401-
def fnmatch_lines(self, lines2):
1394+
def fnmatch_lines(self, lines2: Sequence[str]) -> None:
14021395
"""Search captured text for matching lines using ``fnmatch.fnmatch``.
14031396
14041397
The argument is a list of lines which have to match and can use glob
@@ -1408,7 +1401,7 @@ def fnmatch_lines(self, lines2):
14081401
__tracebackhide__ = True
14091402
self._match_lines(lines2, fnmatch, "fnmatch")
14101403

1411-
def re_match_lines(self, lines2):
1404+
def re_match_lines(self, lines2: Sequence[str]) -> None:
14121405
"""Search captured text for matching lines using ``re.match``.
14131406
14141407
The argument is a list of lines which have to match using ``re.match``.
@@ -1417,9 +1410,16 @@ def re_match_lines(self, lines2):
14171410
The matches and non-matches are also shown as part of the error message.
14181411
"""
14191412
__tracebackhide__ = True
1420-
self._match_lines(lines2, lambda name, pat: re.match(pat, name), "re.match")
1413+
self._match_lines(
1414+
lines2, lambda name, pat: bool(re.match(pat, name)), "re.match"
1415+
)
14211416

1422-
def _match_lines(self, lines2, match_func, match_nickname):
1417+
def _match_lines(
1418+
self,
1419+
lines2: Sequence[str],
1420+
match_func: Callable[[str, str], bool],
1421+
match_nickname: str,
1422+
) -> None:
14231423
"""Underlying implementation of ``fnmatch_lines`` and ``re_match_lines``.
14241424
14251425
:param list[str] lines2: list of string patterns to match. The actual
@@ -1465,23 +1465,27 @@ def _match_lines(self, lines2, match_func, match_nickname):
14651465
self._fail(msg)
14661466
self._log_output = []
14671467

1468-
def no_fnmatch_line(self, pat):
1468+
def no_fnmatch_line(self, pat: str) -> None:
14691469
"""Ensure captured lines do not match the given pattern, using ``fnmatch.fnmatch``.
14701470
14711471
:param str pat: the pattern to match lines.
14721472
"""
14731473
__tracebackhide__ = True
14741474
self._no_match_line(pat, fnmatch, "fnmatch")
14751475

1476-
def no_re_match_line(self, pat):
1476+
def no_re_match_line(self, pat: str) -> None:
14771477
"""Ensure captured lines do not match the given pattern, using ``re.match``.
14781478
14791479
:param str pat: the regular expression to match lines.
14801480
"""
14811481
__tracebackhide__ = True
1482-
self._no_match_line(pat, lambda name, pat: re.match(pat, name), "re.match")
1482+
self._no_match_line(
1483+
pat, lambda name, pat: bool(re.match(pat, name)), "re.match"
1484+
)
14831485

1484-
def _no_match_line(self, pat, match_func, match_nickname):
1486+
def _no_match_line(
1487+
self, pat: str, match_func: Callable[[str, str], bool], match_nickname: str
1488+
) -> None:
14851489
"""Ensure captured lines does not have a the given pattern, using ``fnmatch.fnmatch``
14861490
14871491
:param str pat: the pattern to match lines
@@ -1502,8 +1506,12 @@ def _no_match_line(self, pat, match_func, match_nickname):
15021506
self._log("{:>{width}}".format("and:", width=wnick), repr(line))
15031507
self._log_output = []
15041508

1505-
def _fail(self, msg):
1509+
def _fail(self, msg: str) -> None:
15061510
__tracebackhide__ = True
15071511
log_text = self._log_text
15081512
self._log_output = []
15091513
pytest.fail(log_text)
1514+
1515+
def str(self) -> str:
1516+
"""Return the entire original text."""
1517+
return "\n".join(self.lines)

testing/test_pytester.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,20 +462,20 @@ def test_linematcher_with_nonlist() -> None:
462462

463463
lm = LineMatcher([])
464464
with pytest.raises(TypeError, match="invalid type for lines2: set"):
465-
lm.fnmatch_lines(set())
465+
lm.fnmatch_lines(set()) # type: ignore[arg-type] # noqa: F821
466466
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
467-
lm.fnmatch_lines({})
467+
lm.fnmatch_lines({}) # type: ignore[arg-type] # noqa: F821
468468
with pytest.raises(TypeError, match="invalid type for lines2: set"):
469-
lm.re_match_lines(set())
469+
lm.re_match_lines(set()) # type: ignore[arg-type] # noqa: F821
470470
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
471-
lm.re_match_lines({})
471+
lm.re_match_lines({}) # type: ignore[arg-type] # noqa: F821
472472
with pytest.raises(TypeError, match="invalid type for lines2: Source"):
473-
lm.fnmatch_lines(Source())
473+
lm.fnmatch_lines(Source()) # type: ignore[arg-type] # noqa: F821
474474
lm.fnmatch_lines([])
475475
lm.fnmatch_lines(())
476476
lm.fnmatch_lines("")
477-
assert lm._getlines({}) == {}
478-
assert lm._getlines(set()) == set()
477+
assert lm._getlines({}) == {} # type: ignore[arg-type,comparison-overlap] # noqa: F821
478+
assert lm._getlines(set()) == set() # type: ignore[arg-type,comparison-overlap] # noqa: F821
479479
assert lm._getlines(Source()) == []
480480
assert lm._getlines(Source("pass\npass")) == ["pass", "pass"]
481481

0 commit comments

Comments
 (0)