Skip to content

Fix SyntaxWarning on Python 3.12+: use raw string for mountain_regex#12

Open
sree-vahn wants to merge 1 commit into
phillipjohnson:masterfrom
sree-vahn:fix-regex-syntaxwarning
Open

Fix SyntaxWarning on Python 3.12+: use raw string for mountain_regex#12
sree-vahn wants to merge 1 commit into
phillipjohnson:masterfrom
sree-vahn:fix-regex-syntaxwarning

Conversation

@sree-vahn

Copy link
Copy Markdown

Problem

mountain_regex in src/path_to_enlightenment.py is defined as a regular string literal containing regex escape sequences (\w, \-, \(, \)), which are invalid escape sequences at the Python string level:

mountain_regex = "(--[\w\ \-\)\(,!=\"\':<>%|;]*)"

On Python 3.12 and newer, every run of the koans prints:

src/path_to_enlightenment.py:27: SyntaxWarning: invalid escape sequence '\w'

This is confusing for learners — the warning contains the word "invalid" and appears right before the koan output, so it's easy to mistake for a failing koan.

Why this shows up now

  • Python 3.6–3.11: invalid escape sequences raise a DeprecationWarning (hidden by default).
  • Python 3.12: upgraded to SyntaxWarning, shown by default (What's New in Python 3.12).
  • A future Python release: planned to become a hard SyntaxError, at which point the koans would stop running entirely.

Fix

Make the pattern a raw string (one-character change). The regex itself is unchanged — re receives the exact same pattern as before, so behavior is identical on all Python versions, and the project stays compatible with Python 2 as documented.

Verified by running python3 src/path_to_enlightenment.py on Python 3.12.10: the warning is gone and the koans behave exactly as before.

🤖 Generated with Claude Code

https://claude.ai/code/session_019xSXfPHE79nquz7yRgb5ku

The regex contains escape sequences like \w that are invalid in a
regular string literal. Python 3.12 surfaces this as a SyntaxWarning
on every run, and it is slated to become a SyntaxError in a future
Python release.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xSXfPHE79nquz7yRgb5ku
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant