Use inspect.getdoc to massage fixture docstrings (#6668)

Ref: https://github.com/pytest-dev/pytest/pull/2575
This commit is contained in:
Daniel Hahler 2020-02-04 03:07:53 +01:00 committed by GitHub
parent 75714ee707
commit 5a4c1b628b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 15 deletions

View File

@ -9,7 +9,6 @@ from collections import Counter
from collections import defaultdict from collections import defaultdict
from collections.abc import Sequence from collections.abc import Sequence
from functools import partial from functools import partial
from textwrap import dedent
from typing import List from typing import List
from typing import Optional from typing import Optional
from typing import Tuple from typing import Tuple
@ -1247,7 +1246,7 @@ def _show_fixtures_per_test(config, session):
else: else:
funcargspec = argname funcargspec = argname
tw.line(funcargspec, green=True) tw.line(funcargspec, green=True)
fixture_doc = fixture_def.func.__doc__ fixture_doc = inspect.getdoc(fixture_def.func)
if fixture_doc: if fixture_doc:
write_docstring(tw, fixture_doc) write_docstring(tw, fixture_doc)
else: else:
@ -1332,7 +1331,7 @@ def _showfixtures_main(config, session):
tw.write(" -- %s" % bestrel, yellow=True) tw.write(" -- %s" % bestrel, yellow=True)
tw.write("\n") tw.write("\n")
loc = getlocation(fixturedef.func, curdir) loc = getlocation(fixturedef.func, curdir)
doc = fixturedef.func.__doc__ or "" doc = inspect.getdoc(fixturedef.func)
if doc: if doc:
write_docstring(tw, doc) write_docstring(tw, doc)
else: else:
@ -1341,18 +1340,8 @@ def _showfixtures_main(config, session):
def write_docstring(tw, doc, indent=" "): def write_docstring(tw, doc, indent=" "):
doc = doc.rstrip() for line in doc.split("\n"):
if "\n" in doc: tw.write(indent + line + "\n")
firstline, rest = doc.split("\n", 1)
else:
firstline, rest = doc, ""
if firstline.strip():
tw.line(indent + firstline.strip())
if rest:
for line in dedent(rest).split("\n"):
tw.write(indent + line + "\n")
class Function(PyobjMixin, nodes.Item): class Function(PyobjMixin, nodes.Item):