SyntaxWarning
PythonWARNINGCommonWarning

Dubious or potentially invalid syntax

Quick Answer

Fix the flagged syntax — SyntaxWarnings often indicate a bug. Use == instead of is for value comparisons; use raw strings for regex patterns.

Production Risk

Medium — SyntaxWarning for invalid escapes becomes a SyntaxError in 3.12, breaking the code.

What this means

Raised for dubious syntax that is currently valid Python but may be unintentional, such as using `is` for value comparisons or invalid escape sequences in strings.

Why it happens
  1. 1Using `is` or `is not` to compare with a literal value (e.g. x is 0)
  2. 2Invalid escape sequence in a string literal (becomes SyntaxError in Python 3.12+)

Fix

Replace is with == for value comparisons

Replace is with == for value comparisons
# Bad — raises SyntaxWarning
if x is 0: ...

# Good
if x == 0: ...

# For regex — use raw strings to avoid escape warnings
import re
pattern = re.compile(r'\d+')  # raw string, no SyntaxWarning

Why this works

`is` tests identity, not equality; using it with literals is almost always a bug. Raw strings prevent unintended escape sequences.

Code examples
Promote to error in testspython
import warnings
warnings.filterwarnings('error', category=SyntaxWarning)
Same error in other languages
Version notes
Python 3.12

Invalid escape sequences that were SyntaxWarning in 3.6–3.11 became SyntaxError in 3.12.

Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors