From fc16751c244639ed59276352a72465d3175be18a Mon Sep 17 00:00:00 2001 From: denivyruck Date: Tue, 6 Jul 2021 16:40:28 +0100 Subject: [PATCH 1/5] DoctTest line numbers not found due to method wrapping. (#8796) --- src/_pytest/doctest.py | 5 +++++ testing/test_doctest.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 870920f5a0d..32baed7eab0 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -506,6 +506,11 @@ def _find_lineno(self, obj, source_lines): """ if isinstance(obj, property): obj = getattr(obj, "fget", obj) + + 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 doctest.DocTestFinder._find_lineno( # type: ignore self, diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 1d33e737830..48c81366854 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -1164,6 +1164,41 @@ def test_continue_on_failure(self, pytester: Pytester): ["*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*"] ) + def test_skipping_wrapped_test(self, pytester): + """ + Issue 8796: INTERNALERROR raised when skipping a decorated DocTest + through pytest_collection_modifyitems. + """ + pytester.makeconftest( + """ + import pytest + from _pytest.doctest import DoctestItem + + def pytest_collection_modifyitems(config, items): + skip_marker = pytest.mark.skip() + + for item in items: + if isinstance(item, DoctestItem): + item.add_marker(skip_marker) + """ + ) + + pytester.makepyfile( + """ + from contextlib import contextmanager + + @contextmanager + def my_config_context(): + ''' + >>> import os + ''' + """ + ) + + result = pytester.runpytest("--doctest-modules") + assert "INTERNALERROR" not in result.stdout.str() + result.assert_outcomes(skipped=1) + class TestDoctestAutoUseFixtures: From 3c45b19cb27a38f178ece13d8df1a809e6cb8813 Mon Sep 17 00:00:00 2001 From: denivyruck Date: Mon, 12 Jul 2021 13:33:06 +0100 Subject: [PATCH 2/5] Add bugfix changelog and author --- AUTHORS | 1 + changelog/8796.bugfix.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog/8796.bugfix.rst diff --git a/AUTHORS b/AUTHORS index 5c3610e699c..56769ff5919 100644 --- a/AUTHORS +++ b/AUTHORS @@ -93,6 +93,7 @@ David Vierra Daw-Ran Liou Debi Mishra Denis Kirisov +Denivy Braiam Rück Dhiren Serai Diego Russo Dmitry Dygalo diff --git a/changelog/8796.bugfix.rst b/changelog/8796.bugfix.rst new file mode 100644 index 00000000000..8150811e8d2 --- /dev/null +++ b/changelog/8796.bugfix.rst @@ -0,0 +1 @@ +Fixed internal error when skipping doctests. \ No newline at end of file From 4afc828e4f62e6cdef482596e0ea111008717c3e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jul 2021 12:34:32 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog/8796.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/8796.bugfix.rst b/changelog/8796.bugfix.rst index 8150811e8d2..1e83f4e2613 100644 --- a/changelog/8796.bugfix.rst +++ b/changelog/8796.bugfix.rst @@ -1 +1 @@ -Fixed internal error when skipping doctests. \ No newline at end of file +Fixed internal error when skipping doctests. From 3c9fe917727d39bf8a280e57f5c1865759f09066 Mon Sep 17 00:00:00 2001 From: denivyruck Date: Sun, 18 Jul 2021 20:39:21 +0100 Subject: [PATCH 4/5] Update _find_lineno docstring --- src/_pytest/doctest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index 32baed7eab0..c588eaea13b 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -500,9 +500,10 @@ class MockAwareDocTestFinder(doctest.DocTestFinder): 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 + is a hackish way to fix it. https://bugs.python.org/issue17446 + + 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) From fcefefc22589fd2aa37d6574d596da8d037dffc2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 18 Jul 2021 19:40:21 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/doctest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index c588eaea13b..1588faefaac 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -501,7 +501,7 @@ class MockAwareDocTestFinder(doctest.DocTestFinder): 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 - + Wrapped Doctests will need to be unwrapped so the correct line number is returned. This will be reported upstream. #8796 """