diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 9ec77978..f334529a 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -445,16 +445,26 @@ class Completion: generally in "Python" code in docstrings, we use the following 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) code_lines = split_lines(string, keepends=True) - if code_lines[-1].startswith('>>>') or code_lines[-1].startswith(' '): - code_lines = [ - re.sub(r'^(>>> ?| +)', '', l) - for l in code_lines - if l.startswith('>>>') or l.startswith(' ') - ] + code_lines = list(iter_relevant_lines(code_lines)) + if code_lines[-1] is not None: + # Some code lines might be None, therefore get rid of that. + code_lines = [c or '\n' for c in code_lines] + module_node = self._inference_state.grammar.parse(''.join(code_lines)) module_value = ModuleValue( self._inference_state, diff --git a/test/test_inference/test_docstring.py b/test/test_inference/test_docstring.py index e45b141f..cf2e1724 100644 --- a/test/test_inference/test_docstring.py +++ b/test/test_inference/test_docstring.py @@ -424,3 +424,18 @@ def test_basic_str_init_signature(Script, disable_typeshed): Foo(''') c, = Script(code).find_signatures() 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'