Python reports SyntaxError: invalid syntax on the line after an actual typo because Python’s parser supports implicit line continuation across open parentheses (, brackets [, and braces {—so an unclosed delimiter on line $N$ causes the parser to treat line $N+1$ as a continuation of the same expression until it encounters a statement keyword or token that cannot legally appear inside that expression.
Basic Example
# Typo on line 3: missing closing ')' at the end of print(...)
def greet(user: str) -> None:
items = [1, 2, 3]
print(f"Hello, {user}", len(items) # <-- Actual typo is HERE (missing ')')
return None # <-- Python highlights THIS line!
# Output in Python:
# File "example.py", line 4
# return None
# ^^^^^^
# SyntaxError: invalid syntax
Key Details & Edge Cases
- Why the parser keeps reading onto the next line:
- Inside
( ... ),[ ... ], or{ ... }, Python ignores newline tokens (NEWLINE) so developers can split multi-line function calls, lists, and dictionaries across lines without trailing backslashes\. - When you omit a closing
),], or}on line $N$, Python assumes you intentionally wrapped the expression onto line $N+1$. It only raisesSyntaxErrorwhen it hits the next statement keyword (return,def,if,for,import) or assignment (x = 1)—or reaches the end of the file (EOF)—where that token is illegal inside an open expression.
- Inside
- How Python 3.10+ improved error pointers (and when it still points to line $N+1$):
- Since Python 3.10 (PEP 657 / improved PEG parser diagnostics), Python often points directly to the unclosed delimiter when it reaches EOF (
SyntaxError: '(' was never closed). However, whenever another statement (return,if,x = 1,def) immediately follows the unclosed line, the parser fails at the first illegal token on line $N+1$ because the syntax up to that point could still have been a valid multi-line call or comprehension.
- Since Python 3.10 (PEP 657 / improved PEG parser diagnostics), Python often points directly to the unclosed delimiter when it reaches EOF (
- Other common typos that trigger
SyntaxErroron the following line:- Unclosed triple-quoted strings (
"""or'''): Python absorbs subsequent lines into the string literal until it either hits the next docstring (inverting code and strings!) or reaches EOF (SyntaxError: unterminated triple-quoted string literal). - Missing colon
:at the end of a compound header (if,for,while,def,class,try): Writingif x > 0without:makes Python keep scanning onto the indented body on line $N+1$, flagging the indented statement as invalid syntax. - Missing comma
,between items in a multi-line list, tuple, dict, or function call: Forgetting a comma after"apple"on line $N$ causes Python to reportSyntaxError: invalid syntax. Perhaps you forgot a comma?on"banana"on line $N+1$. - Trailing backslash
\followed by whitespace or a comment: Inx = 1 + \ # comment, the comment breaks line continuation, causing the next line to fail.
- Unclosed triple-quoted strings (
- Fast debugging rule of thumb:
- Whenever
SyntaxErrorpoints to the very beginning of a completely valid line (return,if,def,for, or a variable assignment), immediately inspect the immediately preceding non-empty line for an unclosed(,[,{, quote, or missing:.
- Whenever
Comparison of Common Multi-Line Syntax Typos & Where Python Reports Them
| Actual Typo on Line $N$ | What the Parser Sees on Line $N+1$ | Typical Error Message & Line Reported | How to Fix |
|---|---|---|---|
Unclosed (, [, or { (print(x) |
Treats line $N+1$ (return y) as part of the open call/container |
SyntaxError: invalid syntax on line $N+1$ (or '(' was never closed at EOF) |
Add the matching closing ), ], or } at the end of line $N$. |
Missing , in multi-line list/dict/call |
Two adjacent literals/expressions without a separator | SyntaxError: invalid syntax. Perhaps you forgot a comma? on line $N+1$ |
Insert , at the end of the preceding element on line $N$. |
Missing : on if / for / def header |
Indented suite token arrives before header terminates | SyntaxError: expected ':' (3.10+) or invalid syntax on line $N+1$ |
Append : to the end of the compound statement header on line $N$. |
Unclosed """ / ''' docstring |
Consumes code lines as string text until next """ or EOF |
SyntaxError on the next string/code boundary or EOF |
Close the triple-quoted string literal on the intended line. |
You must log in or register to comment.
