]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 25 Nov 2023 17:39:16 +0000 (18:39 +0100)
committerGitHub <noreply@github.com>
Sat, 25 Nov 2023 17:39:16 +0000 (17:39 +0000)
gh-94722: fix DocTest.__eq__ for case of no line number on one side (GH-112385)
(cherry picked from commit fbb9027a037ff1bfaf3f596df033ca45743ee980)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/doctest.py
Lib/test/test_doctest.py
Misc/NEWS.d/next/Library/2023-11-24-21-00-24.gh-issue-94722.GMIQIn.rst [new file with mode: 0644]

index 21abe475ef85f5509e68f5eaaa213235001b1312..5012b24b98a3398710da28a4b337c59c01727cae 100644 (file)
@@ -569,9 +569,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
index 617529dded3b7382976c6a0430153f6a6bcb1a62..6c0da0b156a5d2b72188823647514c0f2ae384a2 100644 (file)
@@ -416,6 +416,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 (file)
index 0000000..41bd57f
--- /dev/null
@@ -0,0 +1,2 @@
+Fix bug where comparison between instances of :class:`~doctest.DocTest` fails if
+one of them has ``None`` as its lineno.