]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-61648: Detect line numbers of properties in doctests (GH-113161) (GH-113165)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 15 Dec 2023 13:38:45 +0000 (14:38 +0100)
committerGitHub <noreply@github.com>
Fri, 15 Dec 2023 13:38:45 +0000 (13:38 +0000)
(cherry picked from commit 8f8f0f97e126db9ca470fd7e7b2944c150db6305)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/doctest.py
Lib/test/doctest_lineno.py
Lib/test/test_doctest.py
Misc/NEWS.d/next/Library/2023-12-15-12-35-28.gh-issue-61648.G-4pz0.rst [new file with mode: 0644]

index 5012b24b98a3398710da28a4b337c59c01727cae..fc9ec65805346e5a4de8605baee68eef65acf9cf 100644 (file)
@@ -1114,6 +1114,8 @@ class DocTestFinder:
 
         # Find the line number for functions & methods.
         if inspect.ismethod(obj): obj = obj.__func__
+        if isinstance(obj, property):
+            obj = obj.fget
         if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
             # We don't use `docstring` var here, because `obj` can be changed.
             obj = obj.__code__
index 729a68aceaa99013f527ce0e5dce3f183c6a5a95..677c569cf710ebd2e395d8e93463672c92373e13 100644 (file)
@@ -49,5 +49,21 @@ class MethodWrapper:
         'method_with_doctest'
         """
 
+    @classmethod
+    def classmethod_with_doctest(cls):
+        """
+        This has a doctest!
+        >>> MethodWrapper.classmethod_with_doctest.__name__
+        'classmethod_with_doctest'
+        """
+
+    @property
+    def property_with_doctest(self):
+        """
+        This has a doctest!
+        >>> MethodWrapper.property_with_doctest.__name__
+        'property_with_doctest'
+        """
+
 # https://github.com/python/cpython/issues/99433
 str_wrapper = object().__str__
index 6c0da0b156a5d2b72188823647514c0f2ae384a2..71e8841533142c8f9e407bfed119c9ef1201709e 100644 (file)
@@ -673,9 +673,11 @@ It used to be broken for quite some time until `bpo-28249`.
        30  test.doctest_lineno.ClassWithDoctest
      None  test.doctest_lineno.ClassWithoutDocstring
      None  test.doctest_lineno.MethodWrapper
+       53  test.doctest_lineno.MethodWrapper.classmethod_with_doctest
        39  test.doctest_lineno.MethodWrapper.method_with_docstring
        45  test.doctest_lineno.MethodWrapper.method_with_doctest
      None  test.doctest_lineno.MethodWrapper.method_without_docstring
+       61  test.doctest_lineno.MethodWrapper.property_with_doctest
         4  test.doctest_lineno.func_with_docstring
        12  test.doctest_lineno.func_with_doctest
      None  test.doctest_lineno.func_without_docstring
diff --git a/Misc/NEWS.d/next/Library/2023-12-15-12-35-28.gh-issue-61648.G-4pz0.rst b/Misc/NEWS.d/next/Library/2023-12-15-12-35-28.gh-issue-61648.G-4pz0.rst
new file mode 100644 (file)
index 0000000..c841e5c
--- /dev/null
@@ -0,0 +1 @@
+Detect line numbers of properties in doctests.