Skip to content

Commit b10ab02

Browse files
committed
Use TypeError instead of AssertionError for no sequence
Improve/extends tests.
1 parent 09a0e45 commit b10ab02

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/_pytest/pytester.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,8 @@ def _match_lines(self, lines2, match_func, match_nickname):
14301430
:param str match_nickname: the nickname for the match function that
14311431
will be logged to stdout when a match occurs
14321432
"""
1433-
assert isinstance(lines2, collections.abc.Sequence)
1433+
if not isinstance(lines2, collections.abc.Sequence):
1434+
raise TypeError("invalid type for lines2: {}".format(type(lines2).__name__))
14341435
lines2 = self._getlines(lines2)
14351436
lines1 = self.lines[:]
14361437
nextline = None

testing/test_pytester.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,26 @@ def test_timeout():
458458

459459
def test_linematcher_with_nonlist() -> None:
460460
"""Test LineMatcher with regard to passing in a set (accidentally)."""
461+
from _pytest._code.source import Source
462+
461463
lm = LineMatcher([])
462-
with pytest.raises(AssertionError):
464+
with pytest.raises(TypeError, match="invalid type for lines2: set"):
463465
lm.fnmatch_lines(set())
464-
with pytest.raises(AssertionError):
466+
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
465467
lm.fnmatch_lines({})
468+
with pytest.raises(TypeError, match="invalid type for lines2: set"):
469+
lm.re_match_lines(set())
470+
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
471+
lm.re_match_lines({})
472+
with pytest.raises(TypeError, match="invalid type for lines2: Source"):
473+
lm.fnmatch_lines(Source())
466474
lm.fnmatch_lines([])
467475
lm.fnmatch_lines(())
476+
lm.fnmatch_lines("")
468477
assert lm._getlines({}) == {}
469478
assert lm._getlines(set()) == set()
479+
assert lm._getlines(Source()) == []
480+
assert lm._getlines(Source("pass\npass")) == ["pass", "pass"]
470481

471482

472483
def test_linematcher_match_failure() -> None:

0 commit comments

Comments
 (0)