From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 15 Dec 2023 13:44:45 +0000 (+0100) Subject: [3.12] gh-61648: Detect line numbers of properties in doctests (GH-113161) (GH-113164) X-Git-Tag: v3.12.2~309 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ded4307fc1abf2617a1c88abcc7ff352375ea47b;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-61648: Detect line numbers of properties in doctests (GH-113161) (GH-113164) (cherry picked from commit 8f8f0f97e126db9ca470fd7e7b2944c150db6305) Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/doctest.py b/Lib/doctest.py index 12966372f28f..4630e4007e78 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1120,6 +1120,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__ diff --git a/Lib/test/doctest_lineno.py b/Lib/test/doctest_lineno.py index 729a68aceaa9..677c569cf710 100644 --- a/Lib/test/doctest_lineno.py +++ b/Lib/test/doctest_lineno.py @@ -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__ diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index e2f2a8bdd381..9b9f17cb77c5 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -671,9 +671,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 index 000000000000..c841e5c7f768 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-12-15-12-35-28.gh-issue-61648.G-4pz0.rst @@ -0,0 +1 @@ +Detect line numbers of properties in doctests.