Skip to content

Preserve backslashes in generated Python stub docstrings#2218

Open
cupkk wants to merge 2 commits into
huggingface:mainfrom
cupkk:fix/pretokenizer-stub-regex-escapes
Open

Preserve backslashes in generated Python stub docstrings#2218
cupkk wants to merge 2 commits into
huggingface:mainfrom
cupkk:fix/pretokenizer-stub-regex-escapes

Conversation

@cupkk

@cupkk cupkk commented Jul 18, 2026

Copy link
Copy Markdown

The stub generator writes introspected runtime docstrings as ordinary Python triple-quoted strings. Backslash sequences are therefore interpreted a second time: regex escapes such as \s raise SyntaxWarning, while valid sequences such as \n silently change the generated docstring.

This escapes backslashes in module, class, function, and attribute docstrings immediately before stub emission. Runtime docstrings remain human-readable, while parsed stub docstrings preserve the same text. The affected generated normalizer and pre-tokenizer stubs are refreshed.

Validation:

  • cargo test --manifest-path bindings/python/tools/stub-gen/Cargo.toml
  • python -W error::SyntaxWarning -m compileall -q bindings/python/py_src/tokenizers
  • full stub generation with the local extension
  • pytest --import-mode=importlib tests/bindings -q (171 passed)

Fixes #1791

Copilot AI review requested due to automatic review settings July 18, 2026 23:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses SyntaxWarning: invalid escape sequence errors raised by Python 3.12+ when compiling the generated bindings/python/py_src/tokenizers/pre_tokenizers.pyi, caused by regex examples containing \w / \s inside non-raw Python docstrings.

Changes:

  • Escapes backslashes in two Rust binding doc-comment regex examples so the generated Python stub no longer contains invalid escape sequences.
  • Updates the generated pre_tokenizers.pyi to reflect the new escaped docstrings.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
bindings/python/src/pre_tokenizers.rs Updates doc-comment regex examples by doubling backslashes.
bindings/python/py_src/tokenizers/pre_tokenizers.pyi Regenerates stubs so docstrings compile cleanly under -W error::SyntaxWarning.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bindings/python/src/pre_tokenizers.rs Outdated
Comment on lines 337 to 339
/// This pre-tokenizer splits on word boundaries according to the ``\\w+|[^\\w\\s]+``
/// regex pattern. It splits on word characters or characters that aren't words or
/// whitespaces (punctuation such as hyphens, apostrophes, commas, etc.).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I moved the escaping to the stub-generation boundary in 2a5950c and restored the Rust doc comments. The generator now escapes introspected docstrings only before emitting ordinary Python string literals, so runtime .__doc__ still shows a copyable single-backslash regex while the generated stub contains the required source escape. I also covered module/class/function/attribute docstrings and refreshed other affected examples such as \n and \u0301.

Comment thread bindings/python/src/pre_tokenizers.rs Outdated
/// [('one', (0, 3)), ('two', (4, 7)), ('three', (8, 13))]
/// >>> # Split using a regex, keeping the delimiter isolated
/// >>> Split(Regex(r"\s+"), behavior="isolated").pre_tokenize_str("hello world")
/// >>> Split(Regex(r"\\s+"), behavior="isolated").pre_tokenize_str("hello world")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I moved the escaping to the stub-generation boundary in 2a5950c and restored the Rust doc comments. The generator now escapes introspected docstrings only before emitting ordinary Python string literals, so runtime .__doc__ still shows a copyable single-backslash regex while the generated stub contains the required source escape. I also covered module/class/function/attribute docstrings and refreshed other affected examples such as \n and \u0301.

@cupkk cupkk changed the title Fix regex escapes in generated pre-tokenizer stubs Preserve backslashes in generated Python stub docstrings Jul 18, 2026
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.

SyntaxWarning: invalid escape sequence '\w'

2 participants