summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-11-25 17:23:43 (GMT)
committerGitHub <noreply@github.com>2023-11-25 17:23:43 (GMT)
commitfbb9027a037ff1bfaf3f596df033ca45743ee980 (patch)
tree3185087b2f2589a7e9ca05def92bafa5c3c30320
parent19a1fc1b3df30f64450d157dc3a5d40c992e347f (diff)
downloadcpython-fbb9027a037ff1bfaf3f596df033ca45743ee980.zip
cpython-fbb9027a037ff1bfaf3f596df033ca45743ee980.tar.gz
cpython-fbb9027a037ff1bfaf3f596df033ca45743ee980.tar.bz2
gh-94722: fix DocTest.__eq__ for case of no line number on one side (#112385)
-rw-r--r--Lib/doctest.py6
-rw-r--r--Lib/test/test_doctest.py17
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-24-21-00-24.gh-issue-94722.GMIQIn.rst2
3 files changed, 23 insertions, 2 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 2f14aa0..d109b6c 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -591,9 +591,11 @@ class DocTest:
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
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index cb4e215..772dbd1 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -413,6 +413,23 @@ Compare `DocTest`:
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`:
diff --git a/Misc/NEWS.d/next/Library/2023-11-24-21-00-24.gh-issue-94722.GMIQIn.rst b/Misc/NEWS.d/next/Library/2023-11-24-21-00-24.gh-issue-94722.GMIQIn.rst
new file mode 100644
index 0000000..41bd57f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-24-21-00-24.gh-issue-94722.GMIQIn.rst
@@ -0,0 +1,2 @@
+Fix bug where comparison between instances of :class:`~doctest.DocTest` fails if
+one of them has ``None`` as its lineno.