Skip to content

gh-94722: fix DocTest.__eq__ for case of no line number on one side #112385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,11 @@ def __hash__(self):
def __lt__(self, other):
if not isinstance(other, DocTest):
return NotImplemented
return ((self.name, self.filename, self.lineno, id(self))
self_lno = self.lineno if self.lineno is not None else -1
other_lno = other.lineno if other.lineno is not None else -1
return ((self.name, self.filename, self_lno, id(self))
<
(other.name, other.filename, other.lineno, id(other)))
(other.name, other.filename, other_lno, id(other)))

######################################################################
## 3. DocTestParser
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,23 @@ def test_DocTest(): r"""
False
>>> test != other_test
True
>>> test < other_test
False
>>> other_test < test
True

Test comparison with lineno None on one side

>>> no_lineno = parser.get_doctest(docstring, globs, 'some_test',
... 'some_test', None)
>>> test.lineno is None
False
>>> no_lineno.lineno is None
True
>>> test < no_lineno
False
>>> no_lineno < test
True

Compare `DocTestCase`:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where comparison between instances of :class:`~doctest.DocTest` fails if
one of them has ``None`` as its lineno.