0

Let's say I have the following code:

def incr(d, a)
    r = {}
    for key, value in d.items():
        if key != a:
            value += 1
        r[key] = value
    return r
def decr(d, a)
    r = {}
    for key, value in d.items():
        if key == a:
            value -= 1
        r[key] = value
    retur r

As it may be seen, it misses : in both of the definitions and also contains retur instead of return on the last line. However, if I run it through the pyflakes (something like pyflakes test.py) it only reports the first one of all the errors:

$ pyflakes test.py
test.py:9:15: invalid syntax
def incr(d, a)
              ^

As soon as I fix the first one, it moves to the next one on the second run:

$ pyflakes test.py
test.py:18:15: invalid syntax
def decr(d, a)
              ^

So, my question would be am I missing something, or is it possible to show all the possible errors at once?

(As a matter of fact, I'm trying to make use of syntastic plugin for vim -- but because of the behaviour described above, syntastic itself displays only the first error in vim's location list...)

Thanks.

A S
  • 1,195
  • 2
  • 12
  • 26
  • 2
    Syntactic validity of a given line in a (procedural) program can depend on the lines before it. For this reason "all possible errors" doesn't make as much sense as you seem to believe. When compilers, linters, and the like run into a syntax error, they need to get back to a known good state before they can look for more errors. This recovery is a well-known (and well understood) hard problem. For some languages it has an easy solution; in the vast majority of cases, the solution is unknown, or doesn't exist entirely. That's why some compilers simply bail out after the first error. – lcd047 Feb 19 '16 at 20:03

1 Answers1

3

Pyflakes is not intended to check for syntax errors. It is a tool used to check for mistakes that violate coding standards, which could go undetected because the code would still run. For example, unused imports or variables.

The syntax errors are thrown by the python interpreter, not the pyflakes library.

klob
  • 106
  • 4
  • OK, but if I use something like `python -m py_compile test.py` it also shows me only the first possible error: `SyntaxError: ('invalid syntax', ('test.py', 18, 15, 'def decr(d, a)\n'))`. So, is there any way to see all the syntax errors at once? – A S Feb 19 '16 at 18:57
  • When you run the python code, the python interpreter needs to compile it before it can execute anything. This will fail upon the first syntax error, as you've seen. If you want to see all the syntax at once, text editor tools (such as syntastic) are good for that. They way they work is to show icons in the editor indicating errors. If it's configured correctly, you'll see them while writing your code, not executing it. Is that what you're looking for when using that tool? – klob Feb 19 '16 at 19:40