Skip to content

fix(core): correct invalid string-literal escape sequences - #3116

Open
Bartok9 wants to merge 1 commit into
eosphoros-ai:mainfrom
Bartok9:fix/invalid-escape-sequences
Open

fix(core): correct invalid string-literal escape sequences#3116
Bartok9 wants to merge 1 commit into
eosphoros-ai:mainfrom
Bartok9:fix/invalid-escape-sequences

Conversation

@Bartok9

@Bartok9 Bartok9 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes several string literals that used unrecognized backslash escape sequences (\_, \d, \], \ ).
  • These produce SyntaxWarning: invalid escape sequence on Python 3.12+ and become a hard SyntaxError in newer CPython, where a single offending literal can prevent the whole module from importing.

Root Cause

Symptom — Compiling these modules emits SyntaxWarning: "\_" is an invalid escape sequence ... (and \d, \], \ ). On a CPython version that has promoted these to errors, importing the affected module fails outright.

Root cause — In a normal (non-raw) Python string, a backslash followed by a character that is not a recognized escape (\n, \t, ...) is a deprecated "invalid escape sequence." The affected literals were intended to contain a literal backslash:

  • api_call.py:123.replace("\_", "_") meant the two-character text \_.
  • conn_vertica.py (3 sites) — SQL LIKE 'v\_%' (escaped underscore in a LIKE pattern).
  • pdf.py (2 sites) — regex fragments \d, \/ embedded in plain (non-r) strings.
  • websearch_action.py:67 — a \] inside a character class in a plain string concatenated onto an r"..." prefix.
  • app_operator.py:172 — ASCII-art (\ in a diagram) inside a plain docstring.

Evidence — Compiling each file on Python 3.14 with warnings enabled:

packages/dbgpt-core/src/dbgpt/agent/util/api_call.py:123: "\_" is an invalid escape sequence
packages/dbgpt-core/src/dbgpt/agent/expand/actions/websearch_action.py:67: "\]" is an invalid escape sequence
packages/dbgpt-ext/src/dbgpt_ext/datasource/rdbms/conn_vertica.py:101: "\_" is an invalid escape sequence
packages/dbgpt-ext/src/dbgpt_ext/datasource/rdbms/conn_vertica.py:295: "\_" is an invalid escape sequence
packages/dbgpt-ext/src/dbgpt_ext/rag/knowledge/pdf.py:307: "\d" is an invalid escape sequence
packages/dbgpt-ext/src/dbgpt_ext/rag/knowledge/pdf.py:476: "\d" is an invalid escape sequence
packages/dbgpt-app/src/dbgpt_app/scene/operators/app_operator.py:172: "\ " is an invalid escape sequence

Fix + why this level — Escape the backslash explicitly (\\_, \\d, \\], \\/) or mark the ASCII-art docstring raw (r"""). This is the correct layer: the literals already produced the right runtime value via the deprecated implicit behavior, so escaping at the literal makes the intent explicit and forward-compatible without changing any resulting string value. Suppressing the warning globally would have been the wrong level — it hides future breakage instead of removing the cause.

Scope / risk — Touches only string-literal spelling in 5 files; the produced string values are byte-for-byte identical (verified below). No logic, control flow, or public API changes. The SQL LIKE 'v\\_%' and the regex fragments evaluate to exactly the same patterns as before.

Verification

Each edited literal was confirmed to evaluate to the identical string before and after:

api_call  \_  -> identical: True  ('\\_')
vertica   v\_%  -> identical: True  ('v\\_%')
pdf       \d  -> identical: True
pdf end_re ^(?:\d|\\|\/|...){1,}  -> identical: True
websearch [...[\]{}...]  -> identical: True
app_operator (ascii art)  -> identical: True

After the fix, re-compiling all five files yields 0 invalid-escape warnings.

Real behavior proof

  • Behavior addressed: SyntaxWarning: invalid escape sequence on 7 literals across 5 modules (hard error on newer CPython).
  • Real environment: Python 3.14.6, macOS, fresh upstream/main.
  • Exact command run after the patch:
    python3 -m pytest packages/dbgpt-core/src/dbgpt/util/tests/test_no_invalid_escape.py -q
    
  • Captured output AFTER fix:
    .....                                                                    [100%]
    5 passed in 0.08s
    
  • Captured output BEFORE fix (one file reverted to original):
    E   AssertionError: invalid escape sequence(s) in dbgpt-core/src/dbgpt/agent/util/api_call.py:
    E       line 123: "\_" is an invalid escape sequence. ...
    FAILED ...::test_no_invalid_escape_sequence[dbgpt-core/src/dbgpt/agent/util/api_call.py]
    1 failed in 0.07s
    
  • Regression test: packages/dbgpt-core/src/dbgpt/util/tests/test_no_invalid_escape.py — parametrized over the five files, asserts none emit an invalid-escape SyntaxWarning. It FAILS without the fix and passes with it.
  • What was NOT tested: Runtime behavior of the Vertica connector / PDF parser themselves (they require a live DB / PDF fixtures); this change is provably value-preserving at the literal level, so their behavior is unchanged.

Several string literals used unrecognized backslash escapes (\_, \d, \], \ ) that Python flags with `SyntaxWarning: invalid escape sequence` and which become a hard SyntaxError in newer CPython, so a single offending literal can break module import.

Doubled the backslashes (or marked an ASCII-art docstring raw) so the resulting string values are byte-for-byte identical while the warnings are gone.
@github-actions github-actions Bot added core Module: core fix Bug fixes labels Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Module: core fix Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant