From 245b0a5def8d2d478b0c9bfc28daa5507703f00f Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 6 Jun 2024 10:58:04 -0400 Subject: [PATCH] Only rely on _find_lineno on Python 3.10 and earlier. --- src/_pytest/doctest.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 2d7453b4e..e61781acf 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -511,25 +511,27 @@ class DoctestModule(Module): https://bugs.python.org/issue25532 """ - def _find_lineno(self, obj, source_lines): - """Doctest code does not take into account `@property`, this - is a hackish way to fix it. https://bugs.python.org/issue17446 + if sys.version_info < (3, 11): - Wrapped Doctests will need to be unwrapped so the correct - line number is returned. This will be reported upstream. #8796 - """ - if isinstance(obj, property): - obj = getattr(obj, "fget", obj) + def _find_lineno(self, obj, source_lines): + """Doctest code does not take into account `@property`, this + is a hackish way to fix it. https://bugs.python.org/issue17446 - if hasattr(obj, "__wrapped__"): - # Get the main obj in case of it being wrapped - obj = inspect.unwrap(obj) + Wrapped Doctests will need to be unwrapped so the correct + line number is returned. This will be reported upstream. #8796 + """ + if isinstance(obj, property): + obj = getattr(obj, "fget", obj) - # Type ignored because this is a private function. - return super()._find_lineno( # type:ignore[misc] - obj, - source_lines, - ) + if hasattr(obj, "__wrapped__"): + # Get the main obj in case of it being wrapped + obj = inspect.unwrap(obj) + + # Type ignored because this is a private function. + return super()._find_lineno( # type:ignore[misc] + obj, + source_lines, + ) def _find( self, tests, obj, name, module, source_lines, globs, seen