Module

tstring

Kida Template String support (PEP 750).

Provides t-string tags for Python 3.14+:

  • k: Kida-style interpolation with auto-escaping
  • r: Safe, composable regex patterns with ReDoS validation

Example:

>>> from kida.tstring import k, r
>>> name = "<script>"
>>> k(t"Hello {name}!")  # Auto-escapes
'Hello &lt;script&gt;!'

>>> NAME = r"[a-zA-Z_][a-zA-Z0-9_]*"
>>> INTEGER = r"\d+"
>>> pattern = r(t"{NAME}|{INTEGER}")  # Composes safely
>>> pattern.compile().match("variable_123")
<re.Match object; span=(0, 12), match='variable_123'>

Classes

TemplateProtocol 2

Attributes

Name Type Description
strings tuple[str, ...]
interpolations tuple[Any, ...]
TemplateLibProtocol 1

Attributes

Name Type Description
Template type[TemplateProtocol]
PatternError 0
Error raised when a regex pattern is invalid or unsafe.

Error raised when a regex pattern is invalid or unsafe.

ComposablePattern 6
A composable regex pattern with safety validation. **ComposablePattern wraps a regex pattern strin…

A composable regex pattern with safety validation.

ComposablePattern wraps a regex pattern string and provides:

  • Lazy compilation (pattern is compiled on first use)
  • ReDoS validation at creation time
  • Safe composition via the | operator

Attributes

Name Type Description
pattern

The raw regex pattern string

Methods

pattern 0 str
The raw regex pattern string.
property
def pattern(self) -> str
Returns
str
compile 1 re.Pattern[str]
Compile the pattern to a regex object.
def compile(self, flags: int = 0) -> re.Pattern[str]
Parameters
Name Type Description
flags

Regex flags (re.IGNORECASE, re.MULTILINE, etc.)

Default:0
Returns
re.Pattern[str] Compiled re.Pattern object
Internal Methods 3
__init__ 1
Create a composable pattern.
def __init__(self, pattern: str) -> None
Parameters
Name Type Description
pattern

The regex pattern string

__or__ 1 ComposablePattern
Combine patterns with alternation: pattern1 | pattern2. Both patterns are wrap…
def __or__(self, other: ComposablePattern | str) -> ComposablePattern

Combine patterns with alternation: pattern1 | pattern2.

Both patterns are wrapped in non-capturing groups to prevent group interference.

Parameters
Name Type Description
other
Returns
ComposablePattern
__repr__ 0 str
def __repr__(self) -> str
Returns
str

Functions

_validate_redos_safety 1 None
Validate pattern doesn't contain known ReDoS vulnerabilities.
def _validate_redos_safety(pattern: str) -> None
Parameters
Name Type Description
pattern str
r 1 ComposablePattern
The `r` tag for composable regex patterns. Composes regex patterns safely by w…
def r(template: TemplateProtocol) -> ComposablePattern

Thertag for composable regex patterns.

Composes regex patterns safely by wrapping interpolated values in non-capturing groups. This prevents group index collision and quantifier interference.

Parameters
Name Type Description
template TemplateProtocol

A t-string template with pattern interpolations

Returns
ComposablePattern
k 1 str
The `k` tag for Kida template strings. Processes a PEP 750 t-string with autom…
def k(template: TemplateProtocol) -> str

Thektag for Kida template strings.

Processes a PEP 750 t-string with automatic HTML escaping. Values are escaped unless they implement__html__()(Markup).

Type Safety: The TemplateProtocol type hint ensures static type checkers (mypy, pyright) catch misuse likek("string"). Runtime isinstance() check is omitted for performance (~35% faster). Duck typing allows test mocks.

Parameters
Name Type Description
template TemplateProtocol
Returns
str