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

#90: fix no quotes starting docstring or quotes without docstring after signature #91

Merged
merged 1 commit into from
Feb 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pyment/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ class DocString(object):
"""This class represents the docstring"""

def __init__(self, elem_raw, spaces='', docs_raw=None, quotes="'''", input_style=None, output_style=None,
first_line=False, trailing_space=True, **kwargs):
first_line=False, trailing_space=True, before_lim='', **kwargs):
"""
:param elem_raw: raw data of the element (def or class).
:param spaces: the leading whitespaces before the element
Expand All @@ -1181,9 +1181,11 @@ def __init__(self, elem_raw, spaces='', docs_raw=None, quotes="'''", input_style
:param trailing_space: if set, a trailing space will be inserted in places where the user
should write a description
:type trailing_space: boolean
:param before_lim: specify raw or unicode or format docstring type (ie. "r" for r'''... or "fu" for fu'''...)

"""
self.dst = DocsTools()
self.before_lim = before_lim
self.first_line = first_line
self.trailing_space = ''
if trailing_space:
Expand Down Expand Up @@ -1610,12 +1612,14 @@ def _extract_docs_other(self):
lst = self.dst.numpydoc.get_list_key(data, 'attr')
# TODO do something with this?

def parse_docs(self, raw=None):
def parse_docs(self, raw=None, before_lim=''):
"""Parses the docstring

:param raw: the data to parse if not internally provided (Default value = None)
:param before_lim: specify raw or unicode or format docstring type (ie. "r" for r'''... or "fu" for fu'''...)

"""
self.before_lim = before_lim
if raw is not None:
raw = raw.strip()
if raw.startswith('"""') or raw.startswith("'''"):
Expand Down Expand Up @@ -1916,7 +1920,7 @@ def _set_raw(self):
with_space = lambda s: '\n'.join([self.docs['out']['spaces'] + l if i > 0 else l for i, l in enumerate(s.splitlines())])

# sets the description section
raw = self.docs['out']['spaces'] + self.quotes
raw = self.docs['out']['spaces'] + self.before_lim + self.quotes
desc = self.docs['out']['desc'].strip()
if not desc or not desc.count('\n'):
if not self.docs['out']['params'] and not self.docs['out']['return'] and not self.docs['out']['rtype'] and not self.docs['out']['raises']:
Expand Down
21 changes: 18 additions & 3 deletions pyment/pyment.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def _parse(self):
raw = ''
start = 0
end = 0
before_lim = ""

try:
if self.input_file == '-':
Expand Down Expand Up @@ -129,8 +130,15 @@ def _parse(self):
elem_list.append({'docs': e, 'location': (-i, -i)})
else:
if waiting_docs and ('"""' in l or "'''" in l):
# not docstring
if not reading_docs and not (
l[:3] in ['"""', "'''"] or
(l[0] in ['r', 'u', 'f'] and l[1:4] in ['"""', "'''"]) or
(l[0] in ['r', 'u', 'f'] and l[1] in ['r', 'u', 'f'] and l[2:5] in ['"""', "'''"])
):
waiting_docs = False
# start of docstring bloc
if not reading_docs:
elif not reading_docs:
start = i
# determine which delimiter
idx_c = l.find('"""')
Expand All @@ -144,11 +152,18 @@ def _parse(self):
elif idx_c < 0:
lim = "'''"
reading_docs = lim
# check if the docstring starts with 'r', 'u', or 'f' or combination thus extract it
if not l.startswith(lim):
idx_strip_lim = l.find(lim)
idx_abs_lim = ln.find(lim)
# remove and keep the prefix r|f|u
before_lim = l[:idx_strip_lim]
ln = ln[:idx_abs_lim-idx_strip_lim]+ln[idx_abs_lim:]
raw = ln
# one line docstring
if l.count(lim) == 2:
end = i
elem_list[-1]['docs'].parse_docs(raw)
elem_list[-1]['docs'].parse_docs(raw, before_lim)
elem_list[-1]['location'] = (start, end)
reading_docs = None
waiting_docs = False
Expand All @@ -158,7 +173,7 @@ def _parse(self):
elif waiting_docs and lim in l:
end = i
raw += ln
elem_list[-1]['docs'].parse_docs(raw)
elem_list[-1]['docs'].parse_docs(raw, before_lim)
elem_list[-1]['location'] = (start, end)
reading_docs = None
waiting_docs = False
Expand Down
17 changes: 17 additions & 0 deletions tests/issue90.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def foo():
__doc__ = """\
Foo"""


def bar(param):
r"""this is a docstring
"""


def foobar():
u"""this is a docstring
"""


def no_docs():
something = '''bla bla bla'''
26 changes: 26 additions & 0 deletions tests/issue90.py.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--- a/issue90.py
+++ b/issue90.py
@@ -1,17 +1,21 @@
def foo():
+ """ """
__doc__ = """\
Foo"""


def bar(param):
r"""this is a docstring
+
+ :param param:
+
"""


def foobar():
- u"""this is a docstring
- """
+ u"""this is a docstring"""


def no_docs():
+ """ """
something = '''bla bla bla'''
11 changes: 11 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ def func(param): # some comment
result = ''.join(p.diff())
self.assertTrue(result == expected)

def testIssue90(self):
# Title: __doc__ is not well parsed
# If the line after function signature contains triple [double] quotes but is not a docstring
# it will be however considered as if it was and will have side effect.
p = pym.PyComment(absdir('issue90.py'))
p._parse()
f = open(absdir('issue90.py.patch'))
patch = f.read()
f.close()
self.assertEqual(''.join(p.diff()), patch)


def main():
unittest.main()
Expand Down