Python Forum
Odd Syntax Errors - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Odd Syntax Errors (/thread-22060.html)



Odd Syntax Errors - ichabod801 - Oct-27-2019

So I updated some code I've been using for a while. I went to test the new code and got the following syntax error:

Output:
File "minilog.py", line 379 fields = [ for arguments in argument] ^ SyntaxError: invalid syntax
No problem. I stopped working on that line halfway through because I thought of a better way to do it. So I deleted that line and ran it again. I got this syntax error:

Output:
File "minilog.py", line 111 aliases = {'<': 'previous', '>': 'next', 'g': 'goto', 'q': 'quit', 'r': 'rows' 't': 'text', 'v': 'view'} ^ SyntaxError: invalid syntax
Okay, I forgot a comma when adding an item to a dictionary. I do that all the time. I fixed it and ran it, and everything worked fine. But then it hit me. The first error is on line 379, but the second error is before that on line 111. Python checks syntax back to front?


RE: Odd Syntax Errors - Gribouillis - Oct-27-2019

We need an MVCE...


RE: Odd Syntax Errors - buran - Oct-28-2019

is it possible that incidentally removed the comma between the 2 runs? i.e. if you just deleted the line, does it mean fields is not in use at all/later on in the code?


RE: Odd Syntax Errors - ichabod801 - Oct-28-2019

No fields is in use later in the code. I was going to build it in two list comprehensions, and then decided to do an actual loop so I was only going through the base list once. Then I forgot to delete the partial list comprehension I had written.

I guess it's possible I added the alias in between, but I could have sworn I got the first error, fixed it, and got the second error. I will try to recreate the situation later today.


RE: Odd Syntax Errors - ichabod801 - Oct-28-2019

So I made this test:

class Foo(object):

    aliases = {'b': 'bar', 's': 'spam' 'e': 'eggs'}

    def do_bar(self, arguments):
        return [ for argument in arguments]
This was the situation I had with the errors posted above: The first error that was detected was in a method, and the second error that was detected was in a previously defined class attribute of the same class. As you can test for yourself, this snippet does not have the same behavior I mentioned above. When run, the syntax error on line 3 is detected first, as you would expect. I guess I did modify it in between runs, and then spaced out about it.


RE: Odd Syntax Errors - Gribouillis - Oct-28-2019

ichabod801 Wrote:I guess I did modify it in between runs, and then spaced out about it.
As it is the only credible way that we have to reproduce the bug, it is probably what happened.


RE: Odd Syntax Errors - ichabod801 - Oct-28-2019

I wouldn't call it a bug. Whatever happened, it found both syntax errors, just not in the order I was expecting.