Skip to content

Commit d1eb89d

Browse files
authored
Merge pull request #6086 from kondratyev-nv/master
Fix line detection for properties in doctest tests
2 parents 0601f5c + 5e09797 commit d1eb89d

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

changelog/6082.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix line detection for doctest samples inside ``property`` docstrings, as a workaround to `bpo-17446 <https://bugs.python.org/issue17446>`__.

src/_pytest/doctest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ class MockAwareDocTestFinder(doctest.DocTestFinder):
435435
https://bugs.python.org/issue25532
436436
"""
437437

438+
def _find_lineno(self, obj, source_lines):
439+
"""
440+
Doctest code does not take into account `@property`, this is a hackish way to fix it.
441+
442+
https://bugs.python.org/issue17446
443+
"""
444+
if isinstance(obj, property):
445+
obj = getattr(obj, "fget", obj)
446+
return doctest.DocTestFinder._find_lineno(self, obj, source_lines)
447+
438448
def _find(self, tests, obj, name, module, source_lines, globs, seen):
439449
if _is_mocked(obj):
440450
return

testing/test_doctest.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,68 @@ def test(self):
287287
)
288288
)
289289
result = testdir.runpytest("--doctest-modules")
290+
result.stdout.fnmatch_lines(
291+
["*hello*", "006*>>> 1/0*", "*UNEXPECTED*ZeroDivision*", "*1 failed*"]
292+
)
293+
294+
def test_doctest_linedata_on_property(self, testdir):
295+
testdir.makepyfile(
296+
"""
297+
class Sample(object):
298+
@property
299+
def some_property(self):
300+
'''
301+
>>> Sample().some_property
302+
'another thing'
303+
'''
304+
return 'something'
305+
"""
306+
)
307+
result = testdir.runpytest("--doctest-modules")
290308
result.stdout.fnmatch_lines(
291309
[
292-
"*hello*",
293-
"*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*",
294-
"*1/0*",
295-
"*UNEXPECTED*ZeroDivision*",
296-
"*1 failed*",
310+
"*= FAILURES =*",
311+
"*_ [[]doctest[]] test_doctest_linedata_on_property.Sample.some_property _*",
312+
"004 ",
313+
"005 >>> Sample().some_property",
314+
"Expected:",
315+
" 'another thing'",
316+
"Got:",
317+
" 'something'",
318+
"",
319+
"*/test_doctest_linedata_on_property.py:5: DocTestFailure",
320+
"*= 1 failed in *",
321+
]
322+
)
323+
324+
def test_doctest_no_linedata_on_overriden_property(self, testdir):
325+
testdir.makepyfile(
326+
"""
327+
class Sample(object):
328+
@property
329+
def some_property(self):
330+
'''
331+
>>> Sample().some_property
332+
'another thing'
333+
'''
334+
return 'something'
335+
some_property = property(some_property.__get__, None, None, some_property.__doc__)
336+
"""
337+
)
338+
result = testdir.runpytest("--doctest-modules")
339+
result.stdout.fnmatch_lines(
340+
[
341+
"*= FAILURES =*",
342+
"*_ [[]doctest[]] test_doctest_no_linedata_on_overriden_property.Sample.some_property _*",
343+
"EXAMPLE LOCATION UNKNOWN, not showing all tests of that example",
344+
"[?][?][?] >>> Sample().some_property",
345+
"Expected:",
346+
" 'another thing'",
347+
"Got:",
348+
" 'something'",
349+
"",
350+
"*/test_doctest_no_linedata_on_overriden_property.py:None: DocTestFailure",
351+
"*= 1 failed in *",
297352
]
298353
)
299354

0 commit comments

Comments
 (0)