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

IndexError reporting on an empty decorated function under Python 3.7 #640

nedbat opened this issue Feb 8, 2018 · 4 comments
bug Something isn't working report


Copy link

nedbat commented Feb 8, 2018

$ python -V
Python 3.7.0b1

$ cat
def decorator(func):
    return func

class Class:
    def foo(self):

$ coverage run --branch && coverage xml
Traceback (most recent call last):
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/bin/coverage", line 11, in <module>
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 753, in main
    status = CoverageScript().command_line(argv)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 522, in command_line
    total = self.coverage.xml_report(outfile=outfile, **report_args)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 1140, in xml_report
    return, outfile=outfile)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 72, in report
    self.report_files(self.xml_file, morfs)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 91, in report_files
    report_fn(fr, self.coverage._analyze(fr))
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 970, in _analyze
    return Analysis(, it)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 28, in __init__
    self._arc_possibilities = sorted(self.file_reporter.arcs())
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 208, in arcs
    return self.parser.arcs()
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 263, in arcs
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 273, in _analyze_ast
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 545, in analyze
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 1089, in _code_object__ClassDef
    exits = self.add_body_arcs(node.body, from_start=ArcStart(start))
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 666, in add_body_arcs
    prev_starts = self.add_arcs(body_node)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 629, in add_arcs
    return handler(node)
  File "/usr/local/virtualenvs/tmp-e47d674e964684d7/lib/python3.7/site-packages/coverage/", line 834, in _handle_decorated
    body_start = self.line_for_node(node.body[0])
IndexError: list index out of range

Copy link
Owner Author

nedbat commented Feb 9, 2018

Fixed in 99176232199b (bb)

Copy link
Owner Author

nedbat commented Feb 10, 2018

Issue #639 was marked as a duplicate of this issue.

Copy link
Owner Author

nedbat commented Feb 10, 2018

This is now available in 4.5.1.

Copy link
Owner Author

nedbat commented Mar 10, 2018

Original comment by Naoki INADA (Bitbucket: methane, GitHub: methane)

We're thinking change again in 3.7b3. In this time, docstring will be moved from attribute to special AST node named DocString. See this example:

source = """\
def foo():

import ast
t = ast.parse(source)

3.6: ... FunctionDef(name='foo', args=arguments(...), body=[Expr(value=Str(s='docstring'))], decorator_list=[], returns=None) ...

3.7b2: ... FunctionDef(name='foo', args=arguments(...), body=[], decorator_list=[], returns=None, docstring='docstring') ...

3.7b3?: ... FunctionDef(name='foo', args=arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[DocString(s='docstring')], decorator_list=[], returns=None) ...

Of course, ast.getdocstring() works in all versions.
With this change, you can get lineno of docstring from the DocString node (while there is bug for now).

We want feedback from AST users. Please post your comment on this issue or Python-Dev mailing list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bug Something isn't working report
None yet

No branches or pull requests

1 participant