Param
Dataclass utilities for serialization, deserialization, and hidden fields.
Dataclass serialization utilities with support for hidden fields and type coercion.
Hide
Sentinel value to mark fields as hidden during serialization.
Source code in shutils/param.py
25 26 27 28 | |
ParamMixin
Mixin for dataclasses that provides JSON string serialization.
Source code in shutils/param.py
54 55 56 57 58 59 60 61 62 63 | |
to_json_str()
Serialize the dataclass to a JSON string.
Returns:
| Type | Description |
|---|---|
|
A JSON string representation of the dataclass. |
Source code in shutils/param.py
57 58 59 60 61 62 63 | |
asdict(obj, skip_private=False)
Convert a dataclass to a dict, handling nested dataclasses, enums, and Hide fields.
Fields whose value is an instance of Hide are omitted from the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The dataclass instance to convert. |
required |
skip_private
|
bool
|
If True, skip fields whose names start with underscore. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary representation of the dataclass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If obj is not a dataclass. |
Source code in shutils/param.py
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 | |
asjson(obj, skip_private=False, *args, **kwargs)
Serialize a dataclass to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The dataclass instance to serialize. |
required |
skip_private
|
bool
|
If True, skip fields starting with underscore. |
False
|
*args
|
Extra positional arguments forwarded to |
()
|
|
**kwargs
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
A JSON string representation of the dataclass. |
Source code in shutils/param.py
99 100 101 102 103 104 105 106 107 108 109 110 111 | |
deref_forwardref(forward_ref, module_name)
Resolve a ForwardRef to its actual type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forward_ref
|
ForwardRef
|
The ForwardRef to resolve. |
required |
module_name
|
str
|
The module name to resolve in. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The resolved type. |
Source code in shutils/param.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
deref_typestr(type_str, module_name)
Resolve a dotted type string to its actual type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_str
|
str
|
Dotted path like "module.ClassName". |
required |
module_name
|
str
|
The base module to import from. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The resolved type. |
Source code in shutils/param.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
dict_to_dataclass(data, cls)
Convert a dictionary to a dataclass instance with type coercion.
Handles ForwardRef resolution, Union types, nested dataclasses, enums, and generic containers (list, dict, tuple).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary with field values. |
required |
cls
|
type[T]
|
The target dataclass type. |
required |
Returns:
| Type | Description |
|---|---|
T
|
An instance of |
Source code in shutils/param.py
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
json_serializer(obj)
JSON serializer that handles Enum values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to serialize. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The enum's value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If obj is not an Enum. |
Source code in shutils/param.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |