Fix SyntaxWarning for invalid escape sequence in Python 3.6+ by jsell-rh · Pull Request #2267 · apache/age
Closes #2268
Problem
When using apache-age-python with Python 3.6+, a SyntaxWarning is raised:
SyntaxWarning: invalid escape sequence '\s'
WHITESPACE = re.compile('\s')
This warning occurs because \s is not a valid Python string escape sequence. While it currently works (Python interprets unrecognized escape sequences as literal backslash + character), this behavior is deprecated.
Solution
Use a raw string literal (r'\s') to properly declare the regex pattern. Raw strings treat backslashes as literal characters, which is the correct way to define regex patterns in Python.
Changes
File: age/age.py
- Line 28: Changed
WHITESPACE = re.compile('\s')toWHITESPACE = re.compile(r'\s')
Testing
- ✅ Existing functionality unchanged (regex pattern behaves identically)
- ✅ Warning eliminated when running with Python 3.12+
- ✅ Forward compatible with Python 3.14+
References
- PEP 701 - Python 3.12 enhanced error messages
- Python Docs: String Literals
- Why does Python log a DeprecationWarning saying "invalid escape sequence"?
- bpo-27364 - Python 3.6 deprecation of invalid escape sequences
Diff:
-WHITESPACE = re.compile('\s') +WHITESPACE = re.compile(r'\s')