Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #8796 - DoctTest line numbers not found due to method wrapping #8861

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ David Vierra
Daw-Ran Liou
Debi Mishra
Denis Kirisov
Denivy Braiam Rück
Dhiren Serai
Diego Russo
Dmitry Dygalo
Expand Down
1 change: 1 addition & 0 deletions changelog/8796.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed internal error when skipping doctests.
10 changes: 8 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,18 @@ 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.
is a hackish way to fix it. https://bugs.python.org/issue17446

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)

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,
Expand Down
35 changes: 35 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down