From abf3ce7785c60d7c5ba3d7bc718f42be5fe867fd Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Braun Date: Fri, 22 May 2026 01:45:14 +0200 Subject: [PATCH] fix(descriptors): silence SyntaxWarning '\d' in TextMatch docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TextMatch class docstring contains an inline regex example `r"\b\d{3}-\d{3}-\d{4}\b"`. The `r"..."` prefix only marks the *example* literal as raw — the docstring itself is a plain string, so Python 3.12+ parses `\b` and `\d` inside it and emits `SyntaxWarning: invalid escape sequence '\d'` on every import. Mark the docstring itself raw (`r""" ... """`). The rendered help text is unchanged and the example regex still displays the same way. Add a regression test that compiles the module under `warnings.simplefilter("error", SyntaxWarning)` so this can't silently regress on a future docstring edit. Co-Authored-By: Claude Opus 4.7 --- src/evidently/descriptors/text_match.py | 7 ++++++- tests/future/descriptors/test_text_match.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/evidently/descriptors/text_match.py b/src/evidently/descriptors/text_match.py index be1edb1ea8..905847d026 100644 --- a/src/evidently/descriptors/text_match.py +++ b/src/evidently/descriptors/text_match.py @@ -115,7 +115,12 @@ def exact_match(self, text: str, items: List[str]) -> bool: class TextMatch(Descriptor): - """ + # The docstring contains a regex example with `\b\d{3}-\d{3}-\d{4}\b`. The + # `r"..."` prefix only marks the *example* literal as raw — the docstring + # itself still parses every backslash, so `\d` triggers + # `SyntaxWarning: invalid escape sequence '\d'` on Python 3.12+. Marking the + # docstring itself raw silences the warning without changing rendered help. + r""" Unified text matching descriptor that handles all word/text matching scenarios. This descriptor replaces multiple legacy text matching features with a single, diff --git a/tests/future/descriptors/test_text_match.py b/tests/future/descriptors/test_text_match.py index 761f511ae8..7cb9f93c95 100644 --- a/tests/future/descriptors/test_text_match.py +++ b/tests/future/descriptors/test_text_match.py @@ -1,8 +1,25 @@ +import py_compile +import warnings + import pandas as pd import pytest from evidently.core.datasets import Dataset from evidently.descriptors import TextMatch +import evidently.descriptors.text_match as text_match_module + + +def test_text_match_module_compiles_without_syntax_warning(tmp_path): + # Regression: the TextMatch docstring contains an `r"\b\d{3}-..."` regex + # example, but the docstring itself wasn't raw, so `\d` triggered + # `SyntaxWarning: invalid escape sequence '\d'` on Python 3.12+. + # py_compile surfaces SyntaxWarnings as PyCompileErrors when warnings are + # promoted to errors, so this is a reliable check that doesn't depend on + # whether the module is already cached / imported elsewhere. + source_path = text_match_module.__file__ + with warnings.catch_warnings(): + warnings.simplefilter("error", SyntaxWarning) + py_compile.compile(source_path, cfile=str(tmp_path / "out.pyc"), doraise=True) @pytest.fixture