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') to WHITESPACE = 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


Diff:

-WHITESPACE = re.compile('\s')
+WHITESPACE = re.compile(r'\s')