Skip to content

Inference cache

πŸ€– AI-Generated Content

This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.

pyvider.cty.conversion.inference_cache

Classes

Functions

get_container_schema_cache

get_container_schema_cache() -> (
    dict[tuple[Any, ...], CtyType[Any]] | None
)

Get the current container schema cache from the context.

Returns:

Type Description
dict[tuple[Any, ...], CtyType[Any]] | None

Cache dictionary if in active context, None otherwise

Source code in pyvider/cty/conversion/inference_cache.py
def get_container_schema_cache() -> dict[tuple[Any, ...], CtyType[Any]] | None:
    """Get the current container schema cache from the context.

    Returns:
        Cache dictionary if in active context, None otherwise
    """
    if _container_schema_cache.is_active():
        # Access internal context var to maintain compatibility
        return _container_schema_cache._context_var.get()
    return None

get_structural_key_cache

get_structural_key_cache() -> (
    dict[int, tuple[Any, ...]] | None
)

Get the current structural key cache from the context.

Returns:

Type Description
dict[int, tuple[Any, ...]] | None

Cache dictionary if in active context, None otherwise

Source code in pyvider/cty/conversion/inference_cache.py
def get_structural_key_cache() -> dict[int, tuple[Any, ...]] | None:
    """Get the current structural key cache from the context.

    Returns:
        Cache dictionary if in active context, None otherwise
    """
    if _structural_key_cache.is_active():
        # Access internal context var to maintain compatibility
        return _structural_key_cache._context_var.get()
    return None

inference_cache_context

inference_cache_context() -> Generator[None]

Provide isolated inference caches for type inference operations.

Creates scoped caches that are automatically cleaned up when exiting the context. Nested contexts reuse the parent cache. Respects the configuration setting for enabling/disabling caches.

Yields:

Type Description
Generator[None]

None (use get_*_cache() functions within context)

Examples:

>>> with inference_cache_context():
...     # Caches are active here
...     result = infer_cty_type_from_raw(data)
... # Caches automatically cleared
Source code in pyvider/cty/conversion/inference_cache.py
@contextmanager
def inference_cache_context() -> Generator[None]:
    """Provide isolated inference caches for type inference operations.

    Creates scoped caches that are automatically cleaned up when exiting
    the context. Nested contexts reuse the parent cache. Respects the
    configuration setting for enabling/disabling caches.

    Yields:
        None (use get_*_cache() functions within context)

    Examples:
        >>> with inference_cache_context():
        ...     # Caches are active here
        ...     result = infer_cty_type_from_raw(data)
        ... # Caches automatically cleared
    """
    from pyvider.cty.config.runtime import CtyConfig

    config = CtyConfig.get_current()
    if not config.enable_type_inference_cache:
        yield
        return

    with _structural_key_cache.scope(), _container_schema_cache.scope():
        yield

with_inference_cache

with_inference_cache(func: F) -> F

Decorator providing isolated inference cache for function execution.

Ensures thread/async safety by providing each invocation with its own cache context via ContextVar-based scoping.

Parameters:

Name Type Description Default
func F

Function to decorate

required

Returns:

Type Description
F

Decorated function with cache context

Examples:

>>> @with_inference_cache
... def infer_type(value):
...     # Has access to inference caches
...     pass
Source code in pyvider/cty/conversion/inference_cache.py
def with_inference_cache(func: F) -> F:
    """Decorator providing isolated inference cache for function execution.

    Ensures thread/async safety by providing each invocation with its own
    cache context via ContextVar-based scoping.

    Args:
        func: Function to decorate

    Returns:
        Decorated function with cache context

    Examples:
        >>> @with_inference_cache
        ... def infer_type(value):
        ...     # Has access to inference caches
        ...     pass
    """

    @wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        with inference_cache_context():
            return func(*args, **kwargs)

    return wrapper  # type: ignore[return-value]