]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-61648: Detect line numbers of properties in doctests (GH-113161)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 15 Dec 2023 13:24:30 +0000 (15:24 +0200)
committerGitHub <noreply@github.com>
Fri, 15 Dec 2023 13:24:30 +0000 (15:24 +0200)
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 d109b6c9e373438323d8d1bdce16d929b5b02156..114aac62a34e95be4c773b5a8cec5569c7dad222 100644 (file)
@@ -1136,6 +1136,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 36328f8086c7ad2d97f12bee612d34ec3e5e4b3f..46a51007f9644d574df494e0c393005abd7d7af4 100644 (file)
@@ -670,9 +670,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.