coala / coala-bears

Bears for coala
https://coala.io/
GNU Affero General Public License v3.0
295 stars 580 forks source link

RadonBear throws exceptions #19

Open sils opened 8 years ago

sils commented 8 years ago

From @sils1297 on February 18, 2016 14:10

[DEBUG][15:10:00] The bear RadonBear raised an exception. If you are the writer of this bear, please make sure to catch all exceptions. If not and this error annoys you, you might want to get in contact with the writer of this bear.

Traceback information is provided below:

Traceback (most recent call last):
  File "/home/lasse/prog/coala/coalib/bears/Bear.py", line 97, in execute
    return list(self.run_bear_from_section(args, kwargs) or [])
  File "/home/lasse/prog/coala/coalib/misc/../../bears/python/RadonBear.py", line 42, in run
    filename, visitor.lineno, visitor.col_offset, visitor.endline)
  File "/home/lasse/prog/coala/coalib/results/SourceRange.py", line 43, in from_values
    return cls(start, end)
  File "/home/lasse/prog/coala/coalib/misc/Decorators.py", line 298, in decorated
    return function(*args, **kwargs)
  File "/home/lasse/prog/coala/coalib/results/SourceRange.py", line 25, in __init__
    TextRange.__init__(self, start, end)
  File "/home/lasse/prog/coala/coalib/misc/Decorators.py", line 298, in decorated
    return function(*args, **kwargs)
  File "/home/lasse/prog/coala/coalib/results/TextRange.py", line 33, in __init__
    raise ValueError("End position can't be less than start position.")
ValueError: End position can't be less than start position.

Copied from original issue: coala-analyzer/coala#1578

sils commented 8 years ago

From @AbdealiJK on February 19, 2016 4:41

Another exception:

[DEBUG][15:10:00] The bear RadonBear raised an exception. If you are the writer of this bear, please make sure to catch all exceptions. If not and this error annoys you, you might want to get in contact with the writer of this bear.

Traceback information is provided below:

Traceback (most recent call last):
  File "/home/lasse/prog/coala/coalib/bears/Bear.py", line 97, in execute
    return list(self.run_bear_from_section(args, kwargs) or [])
  File "/home/lasse/prog/coala/coalib/misc/../../bears/python/RadonBear.py", line 32, in run
    for visitor in radon.complexity.cc_visit("".join(file)):
  File "/usr/lib/python3.5/site-packages/radon/complexity.py", line 93, in cc_visit
    return cc_visit_ast(code2ast(code), **kwargs)
  File "/usr/lib/python3.5/site-packages/radon/visitors.py", line 33, in code2ast
    return ast.parse(source)
  File "/usr/lib/python3.5/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 115
    print '[EXTTEST]', msg % args
                    ^
SyntaxError: Missing parentheses in call to 'print'
sils commented 8 years ago

@rubik hey, apparently radon throws some exceptions at us. I see one of them is a SyntaxError, which makes sense we can catch that, the other is pretty weird, is radon running some of our code?!

sils commented 8 years ago

CC @Makman2 @AbdealiJK from coala for investigation

rubik commented 8 years ago

Thanks for pinging me, I'll investigate. Radon just performs static analysis and doesn't run any code. The error is this one: https://github.com/coala-analyzer/coala/blob/3165f3a890eeec346fcdb24bda463440951a6fe6/coalib/results/TextRange.py#L32

It appears that in some results the end position is less than the start position, which is weird indeed. Can I see the code that's triggering the error?

EDIT: Since Python 2 is being used it's also possible that end is None and in Python 2 the expression None < 42 evaluates to True. However, end shouldn't be None nevertheless.

sils commented 8 years ago

FWIW coala will never work on python 2 though the analyzed code may be written in python 2. Do we have to spawn a python2 interpreter for radon in those cases?

AbdealiLoKo commented 8 years ago

@sils1297 - I'm unable to reproduce this. What did you run the bear on to get this ?

When I run it on coala it doesn't give this error.

sils commented 8 years ago

@AbdealiJK that's a good question I gave pretty moch not the info we need for reproducing it :P for the second thing some python 2 or syntactically wrong python code should do.

I think I ran it on flask master. Could you try that?

rubik commented 8 years ago

@sils1297 Yes, a SyntaxError is always caused by Python 2 code or wrong syntax altogether. Radon can run on both Python 2 and 3, so that's not a problem. However, if one wants to analyze Python 2 code, Radon must be run with Python 2. I'll see what happens on Flask master.

rubik commented 8 years ago

Just ran coala on Flask master. I got no errors. I used this .coafile:

[Default]
files=flask/**/*.py

[python]
bears=RadonBear
AbdealiLoKo commented 8 years ago

I did the same, not able to reproduce.

On Mon, Feb 29, 2016 at 4:47 PM, Michele Lacchia notifications@github.com wrote:

Just ran coala on Flask master. I got no errors. I used this .coafile:

[Default] files=flask/*/.py

[python] bears=RadonBear

— Reply to this email directly or view it on GitHub https://github.com/coala-analyzer/coala-bears/issues/19#issuecomment-190161399 .

sils commented 8 years ago

I had

files = **/*.py
use_spaces = true

[complexity]
bears = RadonBear

from a fresh cloned repository, i.e. no built files yet. Am definitely getting those errors for that.

sils commented 8 years ago

using those version of coala/bears:

sils commented 8 years ago

radon (1.2.3)

sils commented 8 years ago

running it on e7d548595e8f2f03fb58c827bef8abae2f84aa59

rubik commented 8 years ago

Ok, I got it. Sometimes the end position has None in the column field. That causes the error, as can be seen from this example:

In [1]: from coalib.results.TextPosition import TextPosition

In [2]: start, end = TextPosition(107, 0), TextPosition(107, None)

In [3]: end < start
Out[3]: True

Since Radon cannot know the end column (it parses the AST), I suggest that when an end position has a None value in the column field, this is automatically set to 0.

sils commented 8 years ago

@rubik thanks for looking into this! So essentially Radon gives us a source range with a defined start and an undefined end column while we require all sourceranges to be completely defined. Any idea on what would be the most meaningful source range in that case? Replace None by min(start.column, 0) or something?

rubik commented 8 years ago

Exactly. Radon does not provide an end column, so min(start.column, 0) seems reasonable.

sils commented 8 years ago

@rubik thanks, with that we can work. What is the reason this happens anyway?

rubik commented 8 years ago

That's because Radon parses the AST, and each object in the tree has a line/column position. However, that's referring to the start position, while it's not possible to know the end position in the source code, since different code expressions could produce the same AST. Just adding/removing spaces around operators modifies the end position, but the AST of that expression is always the same.

In theory, Radon could know the start line/column of the last AST object. But labelling that as the end position of the code would be wrong and misleading. That's why I've chosen not to add an end column altogether.