Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Odd Syntax Errors
#1
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
We need an MVCE...
Reply
#3
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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.
Reply
#7
I wouldn't call it a bug. Whatever happened, it found both syntax errors, just not in the order I was expecting.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020