Module

config

ContextVar-based parse configuration for Patitas.

Provides thread-local configuration using Python's ContextVars (PEP 567). Config is set once per Markdown instance, read by all parsers in the context.

Thread Safety:

ContextVars are thread-local by design. Each thread has independent storage,
so no locks are needed and race conditions are impossible.

Usage:

# In Markdown class
md = Markdown(plugins=["table", "math"])
html = md("# Hello")  # Sets config internally via ContextVar

# Direct parser usage (advanced)
from patitas.config import set_parse_config, reset_parse_config, ParseConfig

set_parse_config(ParseConfig(tables_enabled=True))
try:
    parser = Parser(source)
    result = parser.parse()
finally:
    reset_parse_config()

# Or use the context manager
with parse_config_context(ParseConfig(tables_enabled=True)):
    parser = Parser(source)
    result = parser.parse()

Classes

ParseConfig 10
Immutable parse configuration. Set once per Markdown instance, read by all parsers in the context.…

Immutable parse configuration.

Set once per Markdown instance, read by all parsers in the context. Frozen dataclass ensures thread-safety (immutable after creation).

Note: source_file is intentionally excluded—it's per-call state, not configuration. It remains on the Parser instance.

Attributes

Name Type Description
tables_enabled bool

Enable GFM table parsing

strikethrough_enabled bool

Enable strikethrough syntax

task_lists_enabled bool

Enable - [ ] task list items

footnotes_enabled bool

Enable [^ref] footnote references

math_enabled bool

Enable $inline$ and $$block$$ math

autolinks_enabled bool

Enable automatic URL linking

directive_registry DirectiveRegistry | None

Registry for directive handlers

strict_contracts bool

Raise errors on directive contract violations

text_transformer Callable[[str], str] | None

Optional callback to transform plain text lines

Methods

from_dict 1 ParseConfig
Create ParseConfig from dictionary. Useful for framework integration where con…
classmethod
def from_dict(cls, config_dict: dict) -> ParseConfig

Create ParseConfig from dictionary.

Useful for framework integration where config may come from external sources (e.g., Bengal's own ParseConfig, YAML files, etc.).

Only includes keys that are valid ParseConfig fields; unknown keys are silently ignored.

Parameters
Name Type Description
config_dict

Dictionary with config values. Keys should match ParseConfig attribute names.

Returns
ParseConfig New ParseConfig instance with values from dict.

Functions

get_parse_config 0 ParseConfig
Get current parse configuration (thread-local). **Thread Safety:** ContextVars…
def get_parse_config() -> ParseConfig

Get current parse configuration (thread-local).

Thread Safety: ContextVars are thread-local by design. Safe to call from any thread.

Returns
ParseConfig
set_parse_config 1 None
Set parse configuration for current context. **Thread Safety:** Only affects t…
def set_parse_config(config: ParseConfig) -> None

Set parse configuration for current context.

Thread Safety: Only affects the current thread's context. Other threads are unaffected.

Parameters
Name Type Description
config ParseConfig

ParseConfig instance to use for this context.

reset_parse_config 0 None
Reset to default configuration. Reuses the module-level _DEFAULT_CONFIG single…
def reset_parse_config() -> None

Reset to default configuration.

Reuses the module-level _DEFAULT_CONFIG singleton, avoiding allocation.

Thread Safety:

Only affects the current thread's context. Other threads are unaffected.
parse_config_context 1 Iterator[None]
Context manager for temporary config changes. Useful for tests and isolated pa…
def parse_config_context(config: ParseConfig) -> Iterator[None]

Context manager for temporary config changes.

Useful for tests and isolated parsing operations.

Thread Safety: Only affects the current thread's context. Properly restores previous config even if an exception is raised.

Parameters
Name Type Description
config ParseConfig

ParseConfig to use within the context.

Returns
Iterator[None]