Some more clarifications around docstrings, see #860

This commit is contained in:
Dave Halter
2020-01-01 01:45:58 +01:00
parent 50c5eb5786
commit cea7a12908
2 changed files with 32 additions and 7 deletions

View File

@@ -445,16 +445,26 @@ class Completion:
generally in "Python" code in docstrings, we use the following generally in "Python" code in docstrings, we use the following
heuristic: heuristic:
- Either - Having an indented block of code
- Having some doctest code that starts with `>>>`
""" """
def iter_relevant_lines(lines):
include_next_line = False
for l in code_lines:
if include_next_line or l.startswith('>>>') or l.startswith(' '):
yield re.sub(r'^( *>>> ?| +)', '', l)
else:
yield None
include_next_line = bool(re.match(' *>>>', l))
string = dedent(string) string = dedent(string)
code_lines = split_lines(string, keepends=True) code_lines = split_lines(string, keepends=True)
if code_lines[-1].startswith('>>>') or code_lines[-1].startswith(' '): code_lines = list(iter_relevant_lines(code_lines))
code_lines = [ if code_lines[-1] is not None:
re.sub(r'^(>>> ?| +)', '', l) # Some code lines might be None, therefore get rid of that.
for l in code_lines code_lines = [c or '\n' for c in code_lines]
if l.startswith('>>>') or l.startswith(' ')
]
module_node = self._inference_state.grammar.parse(''.join(code_lines)) module_node = self._inference_state.grammar.parse(''.join(code_lines))
module_value = ModuleValue( module_value = ModuleValue(
self._inference_state, self._inference_state,

View File

@@ -424,3 +424,18 @@ def test_basic_str_init_signature(Script, disable_typeshed):
Foo(''') Foo(''')
c, = Script(code).find_signatures() c, = Script(code).find_signatures()
assert c.name == 'Foo' assert c.name == 'Foo'
def test_doctest_result_completion(Script):
code = '''\
"""
comment
>>> something = 3
somethi
"""
something_else = 8
'''
c1, c2 = Script(code).complete(line=5)
assert c1.complete == 'ng'
assert c2.complete == 'ng_else'