Skip to content

Capsule Types

Capsule types allow you to encapsulate opaque Python objects within the cty type system. They are useful for wrapping foreign data types that cannot be represented by the standard cty types, such as file handles, database connections, or other external resources.

πŸ€– 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.

The capsule types are: - CtyCapsule - Base capsule type for wrapping arbitrary Python objects with type checking - CtyCapsuleWithOps - Extended capsule type that supports custom operations on encapsulated data - BytesCapsule - Pre-defined capsule type for wrapping raw bytes data

Capsule types provide type safety by validating that encapsulated objects match the expected Python type. They preserve the opaque nature of the data while allowing it to participate in the cty type system for validation, serialization, and other operations.

See also: User Guide: Capsule Types for detailed usage examples.


pyvider.cty.types.capsule

Classes

CtyCapsule

CtyCapsule(capsule_name: str, py_type: type)

Bases: CtyType[Any]

Represents a capsule type in the Cty type system. Capsule types are opaque types that can be used to wrap arbitrary Python objects.

Source code in pyvider/cty/types/capsule.py
def __init__(self, capsule_name: str, py_type: type) -> None:
    super().__init__()
    self.name = capsule_name
    self._py_type = py_type

CtyCapsuleWithOps

CtyCapsuleWithOps(
    capsule_name: str,
    py_type: type,
    *,
    equal_fn: Callable[[Any, Any], bool] | None = None,
    hash_fn: Callable[[Any], int] | None = None,
    convert_fn: Callable[
        [Any, CtyType[Any]], CtyValue[Any] | None
    ]
    | None = None,
)

Bases: CtyCapsule

A CtyCapsule that supports custom operations like equality, hashing, and conversion.

Initializes a CtyCapsule with custom operational functions.

Source code in pyvider/cty/types/capsule.py
def __init__(
    self,
    capsule_name: str,
    py_type: type,
    *,
    equal_fn: Callable[[Any, Any], bool] | None = None,
    hash_fn: Callable[[Any], int] | None = None,
    convert_fn: Callable[[Any, CtyType[Any]], CtyValue[Any] | None] | None = None,
) -> None:
    """
    Initializes a CtyCapsule with custom operational functions.
    """
    super().__init__(capsule_name, py_type)
    self.equal_fn = equal_fn
    self.hash_fn = hash_fn
    self.convert_fn = convert_fn
    self._validate_ops_arity()
Functions