Skip to content

Commit e93af80

Browse files
[3.12] gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385) (#112400)
gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385) (cherry picked from commit fbb9027) Co-authored-by: Irit Katriel <[email protected]>
1 parent 44eb329 commit e93af80

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
@@ -575,9 +575,11 @@ def __hash__(self):
575575
def __lt__(self, other):
576576
if not isinstance(other, DocTest):
577577
return NotImplemented
578-
return ((self.name, self.filename, self.lineno, id(self))
578+
self_lno = self.lineno if self.lineno is not None else -1
579+
other_lno = other.lineno if other.lineno is not None else -1
580+
return ((self.name, self.filename, self_lno, id(self))
579581
<
580-
(other.name, other.filename, other.lineno, id(other)))
582+
(other.name, other.filename, other_lno, id(other)))
581583

582584
######################################################################
583585
## 3. DocTestParser

Lib/test/test_doctest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,23 @@ def test_DocTest(): r"""
414414
False
415415
>>> test != other_test
416416
True
417+
>>> test < other_test
418+
False
419+
>>> other_test < test
420+
True
421+
422+
Test comparison with lineno None on one side
423+
424+
>>> no_lineno = parser.get_doctest(docstring, globs, 'some_test',
425+
... 'some_test', None)
426+
>>> test.lineno is None
427+
False
428+
>>> no_lineno.lineno is None
429+
True
430+
>>> test < no_lineno
431+
False
432+
>>> no_lineno < test
433+
True
417434
418435
Compare `DocTestCase`:
419436
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)