adapters
agent_cover.instrumentation.structures.adapters
Module for converting data structures to dictionaries.
This module provides adapters for converting different data structures (e.g., Pydantic models, dataclasses) into dictionaries. These adapters enable AgentCover to process data from various sources in a unified manner.
Classes
AdapterRegistry
Registry for managing structure adapters.
This registry stores and provides access to different structure adapters, allowing AgentCover to handle various data structures uniformly.
Attributes:
| Name | Type | Description |
|---|---|---|
_adapters |
list[StructureAdapter]
|
A list of StructureAdapter instances. |
Methods:
| Name | Description |
|---|---|
register |
StructureAdapter): Registers a new adapter. |
clear |
Clears all registered adapters. |
get_adapter_for_class |
type) -> Optional[StructureAdapter]: Retrieves the adapter for a given class. |
get_adapter_for_instance |
Any) -> Optional[StructureAdapter]: Retrieves the adapter for a given instance. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
Functions
__init__()
clear()
get_adapter_for_class(cls)
Retrieves the adapter for a given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to find an adapter for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[StructureAdapter]
|
The StructureAdapter instance that can handle the class, or None. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
get_adapter_for_instance(obj)
Retrieves the adapter for a given instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The instance to find an adapter for. |
required |
Returns:
| Type | Description |
|---|---|
Optional[StructureAdapter]
|
The StructureAdapter instance that can handle the instance, or None. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
register(adapter)
Registers a new structure adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
StructureAdapter
|
The StructureAdapter instance to register. |
required |
DataclassAdapter
Bases: StructureAdapter
Adapter for dataclasses.
This adapter is designed to convert dataclasses into dictionaries, allowing AgentCover to analyze and process data from dataclass-based data structures.
Methods:
| Name | Description |
|---|---|
can_handle_class |
type) -> bool: Checks if the adapter can handle a given class. |
can_handle_instance |
Any) -> bool: Checks if the adapter can handle a given instance. |
get_fields |
type) -> dict[str, type]: Retrieves the fields of a class. |
to_dict |
Any) -> dict: Converts a dataclass instance to a dictionary. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
Functions
can_handle_class(cls)
Checks if the given class is a dataclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the class is a dataclass, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
can_handle_instance(obj)
Checks if the given object is an instance of a dataclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the object is a dataclass instance, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
get_fields(cls)
Retrieves the fields of a dataclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The dataclass class. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary of field names and types. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
to_dict(obj)
Converts a dataclass instance to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The dataclass instance. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary representation of the dataclass. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
PydanticAdapter
Bases: StructureAdapter
Adapter for Pydantic models.
This adapter is designed to convert Pydantic models into dictionaries, allowing AgentCover to analyze and process data from Pydantic-based data structures. It supports both Pydantic V1 and V2.
Attributes:
| Name | Type | Description |
|---|---|---|
BaseModel |
The Pydantic BaseModel class (dynamically imported). |
Methods:
| Name | Description |
|---|---|
can_handle_class |
type) -> bool: Checks if the adapter can handle a given class. |
can_handle_instance |
Any) -> bool: Checks if the adapter can handle a given instance. |
get_fields |
type) -> dict[str, type]: Retrieves the fields of a class. |
to_dict |
Any) -> dict: Converts a Pydantic model instance to a dictionary. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
Functions
__init__(pydantic_module=None)
Initializes a PydanticAdapter instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pydantic_module
|
Any
|
Optional. Dependency Injection of the pydantic module. If None, attempts to import it. If passed (e.g., Mock), uses that. |
None
|
Source code in src/agent_cover/instrumentation/structures/adapters.py
can_handle_class(cls)
Checks if the given class is a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the class is a Pydantic model, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
can_handle_instance(obj)
Checks if the given object is an instance of a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the object is a Pydantic model instance, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
get_fields(cls)
Retrieves the fields of a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The Pydantic model class. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary of field names and types. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
to_dict(obj)
Converts a Pydantic model instance to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The Pydantic model instance. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary representation of the Pydantic model. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
StructureAdapter
Bases: ABC
Abstract base class for structure adapters.
Structure adapters are responsible for converting objects of different types into dictionaries, enabling unified handling of various data structures within the AgentCover framework.
Methods:
| Name | Description |
|---|---|
can_handle_class |
type) -> bool: Abstract method to check if the adapter can handle a given class. |
can_handle_instance |
Any) -> bool: Abstract method to check if the adapter can handle a given instance. |
get_fields |
type) -> dict[str, type]: Abstract method to retrieve the fields of a class. |
to_dict |
Any) -> dict: Abstract method to convert an object to a dictionary. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
Functions
can_handle_class(cls)
abstractmethod
Checks if this adapter can handle the given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the adapter can handle the class, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
can_handle_instance(obj)
abstractmethod
Checks if this adapter can handle the given instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The instance to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the adapter can handle the instance, False otherwise. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
get_fields(cls)
abstractmethod
Retrieves the fields of the given class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to inspect. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, type]
|
A dictionary where keys are field names and values are field types. |
Source code in src/agent_cover/instrumentation/structures/adapters.py
to_dict(obj)
abstractmethod
Converts the given object to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to convert. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary representation of the object. |
Functions
get_default_adapter_registry()
Retrieves the default AdapterRegistry instance.
Returns:
| Type | Description |
|---|---|
|
The default AdapterRegistry instance. |