all Python 3Exceptionsexception
SyntaxWarning
built-in exceptions
Warns about dubious syntax (e.g. a suspicious comparison such as x is 1).
Syntax
raise SyntaxWarning
It is a warning, not an error: the compiler prints it once and keeps going, so an except block normally never sees it. The two classic triggers are comparing to a literal with is instead of ==, and an unrecognised escape sequence in a plain string — since Python 3.12 that emits SyntaxWarning, and an r-prefix fixes it. Run with -W error::SyntaxWarning if you want these spots to fail loudly instead of scrolling past.
Examples
try:
raise SyntaxWarning
except SyntaxWarning as e:
print(type(e).__name__) # → SyntaxWarning
See also
Open in the full glossarydocs.python.orgReport an inaccuracy