MCPcopy Create free account
hub / github.com/agentscope-ai/Trinity-RFT / build_dataclass_from_mapping

Function build_dataclass_from_mapping

trinity/common/dataclass_utils.py:7–22  ·  view source on GitHub ↗

Convert a mapping (dict, OmegaConf, etc.) to a dataclass instance, recursively.

(dataclass_type: type[Any], data: Any)

Source from the content-addressed store, hash-verified

5
6
7def build_dataclass_from_mapping(dataclass_type: type[Any], data: Any) -> Any:
8 """
9 Convert a mapping (dict, OmegaConf, etc.) to a dataclass instance, recursively.
10 """
11 if data is None:
12 return None
13 if is_dataclass(data):
14 return data
15 mapping = _normalize_mapping(data)
16 kwargs = {}
17 for dataclass_field in fields(dataclass_type):
18 if dataclass_field.name in mapping:
19 kwargs[dataclass_field.name] = _convert_dataclass_field(
20 dataclass_field.type, mapping[dataclass_field.name]
21 )
22 return dataclass_type(**kwargs)
23
24
25def _normalize_mapping(raw_data: Any) -> dict[str, Any]:

Callers 3

validateMethod · 0.90
from_hf_datasetsFunction · 0.90
_convert_dataclass_fieldFunction · 0.85

Calls 2

_normalize_mappingFunction · 0.85
_convert_dataclass_fieldFunction · 0.85

Tested by

no test coverage detected