diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 23ad7a7a979..e61694d553f 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -505,7 +505,13 @@ def collect(self) -> Iterable[DoctestItem]: import doctest class MockAwareDocTestFinder(doctest.DocTestFinder): - if sys.version_info < (3, 11): + py_ver_info_minor = sys.version_info[:2] + is_find_lineno_broken = ( + py_ver_info_minor < (3, 11) + or (py_ver_info_minor == (3, 11) and sys.version_info.micro < 9) + or (py_ver_info_minor == (3, 12) and sys.version_info.micro < 3) + ) + if is_find_lineno_broken: def _find_lineno(self, obj, source_lines): """On older Pythons, doctest code does not take into account