Skip to content

Commit 8c9f273

Browse files
[3.11] gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385) (#112401)
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 68db0ed commit 8c9f273

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
@@ -569,9 +569,11 @@ def __hash__(self):
569569
def __lt__(self, other):
570570
if not isinstance(other, DocTest):
571571
return NotImplemented
572-
return ((self.name, self.filename, self.lineno, id(self))
572+
self_lno = self.lineno if self.lineno is not None else -1
573+
other_lno = other.lineno if other.lineno is not None else -1
574+
return ((self.name, self.filename, self_lno, id(self))
573575
<
574-
(other.name, other.filename, other.lineno, id(other)))
576+
(other.name, other.filename, other_lno, id(other)))
575577

576578
######################################################################
577579
## 3. DocTestParser

Lib/test/test_doctest.py

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