Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an IndentationError being raised here rather than a SyntaxError?

Why in the following program is an IndentationError being raised rather than SyntaxError?

>>> if True:
... print "just right!"
  File "<stdin>", line 2
    print "just right!"
        ^
IndentationError: Missing parentheses in call to 'print'

To make sure the IDLE wasn't just acting funny, I also tested this code by running it from a normal source file. The same exception type is still being raised. The versions of Python I used to test this were Python 3.5.2 and Python 3.6.1.

It is my understanding that missing parenthesis when using print was considered a SyntaxError, not an IndentationError. The top answer in the post What does “SyntaxError: Missing parentheses in call to 'print'” mean in Python? also seems to support this:

“SyntaxError: Missing parentheses in call to 'print'” is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.

Is this a bug? If so, what's causing it?

like image 288
Christian Dean Avatar asked Aug 09 '17 13:08

Christian Dean


People also ask

Why do I keep getting indentation error in Python?

The cause of Indentation Error in Python Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error.

Is indentation a syntax error?

Syntax Errors Both SyntaxError and IndentationError indicate a problem with the syntax of your program, but an IndentationError is more specific: it always means that there is a problem with how your code is indented.

Why am I getting an indentation error?

The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper.


1 Answers

IndentationError is a subclass of SyntaxError, so technically, this is a syntax error being raised.

You have two errors here. Both the indentation is wrong and you are missing parentheses. It's a bit of a bug, there is code that alters the SyntaxError message when the print special case is detected, and that code still applies for subclasses of SyntaxError (it is applied in the SyntaxError exception constructor).

You can trigger the same error for the TabError exception:

>>> compile('if 1:\n    1\n\tprint "Look ma, tabs!"', '', 'single')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 3
    print "Look ma, tabs!"
                         ^
TabError: Missing parentheses in call to 'print'

The SyntaxError codepath checking for the exec() and print() edge-cases should really only fire for actual SyntaxError instances, not subclasses, as that's just confusing.

I've filed issue 31161 to track this.

like image 184
Martijn Pieters Avatar answered Oct 24 '22 10:10

Martijn Pieters