Skip to content

Commit fbb9027

Browse files
authored
gh-94722: fix DocTest.__eq__ for case of no line number on one side (#112385)
1 parent 19a1fc1 commit fbb9027

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Lib/doctest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,11 @@ def __hash__(self):
591591
def __lt__(self, other):
592592
if not isinstance(other, DocTest):
593593
return NotImplemented
594-
return ((self.name, self.filename, self.lineno, id(self))
594+
self_lno = self.lineno if self.lineno is not None else -1
595+
other_lno = other.lineno if other.lineno is not None else -1
596+
return ((self.name, self.filename, self_lno, id(self))
595597
<
596-
(other.name, other.filename, other.lineno, id(other)))
598+
(other.name, other.filename, other_lno, id(other)))
597599

598600
######################################################################
599601
## 3. DocTestParser

Lib/test/test_doctest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,23 @@ def test_DocTest(): r"""
413413
False
414414
>>> test != other_test
415415
True
416+
>>> test < other_test
417+
False
418+
>>> other_test < test
419+
True
420+
421+
Test comparison with lineno None on one side
422+
423+
>>> no_lineno = parser.get_doctest(docstring, globs, 'some_test',
424+
... 'some_test', None)
425+
>>> test.lineno is None
426+
False
427+
>>> no_lineno.lineno is None
428+
True
429+
>>> test < no_lineno
430+
False
431+
>>> no_lineno < test
432+
True
416433
417434
Compare `DocTestCase`:
418435
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where comparison between instances of :class:`~doctest.DocTest` fails if
2+
one of them has ``None`` as its lineno.

0 commit comments

Comments
 (0)