Module API - Fields
superconf.fields
Fields management
Field
Base class for configuration fields.
A Field represents a single configuration value with optional type casting, default values, and help text. Fields are used as descriptors in configuration classes to define the structure and behavior of configuration values.
Attributes:
Name | Type | Description |
---|---|---|
cast |
The casting function to use for converting raw values. If None, will be determined based on the default value's type. |
Source code in superconf/fields.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
__get__(conf_instance, owner)
Descriptor get method to retrieve the field's value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf_instance
|
The configuration instance this field belongs to. |
required | |
owner
|
The class that owns this descriptor. |
required |
Returns:
Type | Description |
---|---|
The field's value if accessed through an instance, |
|
or the field itself if accessed through the class. |
Source code in superconf/fields.py
__init__(key=None, *, help='', default=NOT_SET, cast=None)
Initialize a new Field instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Name of the value used in file or environment variable. Set automatically by the metaclass. |
None
|
help
|
str
|
Plain-text description of the value. |
''
|
default
|
Default value if none is provided. If left unset, loading a config that fails to provide this value will raise a UnknownConfiguration exception. |
NOT_SET
|
|
cast
|
Callable
|
Callable to cast variable with. Defaults to type of default (if provided), identity if default is not provided or raises TypeError if provided cast is not callable. |
None
|
Source code in superconf/fields.py
__repr__()
Return a string representation of the field.
Returns:
Type | Description |
---|---|
A string showing the field's class name, key, and help text. |
is_container()
Check if this field is a container type.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if this field has a children_class attribute, |
|
indicating it can contain nested configuration values. |
Source code in superconf/fields.py
resolve_value(conf_instance, value=NOT_SET, default=NOT_SET, cast=NOT_SET, loaders=NOT_SET, lookup=True, **kwargs)
Resolve the final value for this field.
This method handles the complex logic of determining the field's value by: 1. Checking for explicitly provided values 2. Looking up values through loaders 3. Falling back to defaults 4. Applying type casting
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf_instance
|
The configuration instance this field belongs to. |
required | |
value
|
Explicitly provided value, takes precedence if set. |
NOT_SET
|
|
default
|
Override for the field's default value. |
NOT_SET
|
|
cast
|
Override for the field's cast function. |
NOT_SET
|
|
loaders
|
List of loader objects to use for value lookup. |
NOT_SET
|
|
lookup
|
Enable loaders, or only use _value instead. |
True
|
|
**kwargs
|
Additional keyword arguments passed to loaders. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing: - The resolved and cast value - A SimpleNamespace containing metadata about the resolution process |
Raises:
Type | Description |
---|---|
CastValueFailure
|
If strict casting is enabled and the value cannot be cast to the desired type. |
Source code in superconf/fields.py
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 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
FieldBool
Bases: Field
A field that stores and validates boolean values.
Uses the AsBoolean cast to convert various string representations to boolean values (e.g., 'yes'/'no', 'true'/'false', '1'/'0').
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsBoolean() for automatic type conversion. |
Source code in superconf/fields.py
FieldConf
Bases: Field
A field that represents a nested configuration.
This field type allows for hierarchical configuration structures by containing another configuration class as its value.
Attributes:
Name | Type | Description |
---|---|---|
children_class |
The configuration class to use for nested values. |
Source code in superconf/fields.py
__init__(children_class, key=None, **kwargs)
Initialize a nested configuration field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
children_class
|
The configuration class to use for nested values. |
required | |
key
|
str
|
Name of the value used in file or environment variable. |
None
|
**kwargs
|
Additional arguments passed to the parent Field class. |
{}
|
Source code in superconf/fields.py
FieldDict
Bases: Field
A field that stores dictionary values.
Uses the AsDict cast to ensure values are proper dictionaries, with support for converting mapping objects.
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsDict() for automatic type conversion. |
Source code in superconf/fields.py
FieldFloat
Bases: Field
A field that stores floating-point values.
Uses Python's built-in float() function to convert values, raising an error if the conversion fails.
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to float for automatic type conversion. |
Source code in superconf/fields.py
FieldInt
Bases: Field
A field that stores integer values.
Uses the AsInt cast to convert string representations to integers, raising an error if the conversion fails.
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsInt() for automatic type conversion. |
Source code in superconf/fields.py
FieldList
Bases: Field
A field that stores list values.
Uses the AsList cast to convert various inputs to lists, including: - Comma-separated strings - Other sequence types - Empty values to empty lists
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsList() for automatic type conversion. |
Source code in superconf/fields.py
FieldOption
Bases: Field
A field that validates values against a predefined set of options.
This field ensures that values are one of a predefined set of options, optionally providing a default if an invalid option is given.
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsOption for option validation and conversion. |
Source code in superconf/fields.py
__init__(options, default_option=FAIL, key=None, **kwargs)
Initialize an option field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
Dictionary mapping valid input values to their corresponding options. |
required | |
default_option
|
The option to use when an invalid value is provided. If set to FAIL, raises an error for invalid values. |
FAIL
|
|
key
|
str
|
Name of the value used in file or environment variable. |
None
|
**kwargs
|
Additional arguments passed to the parent Field class. |
{}
|
Raises:
Type | Description |
---|---|
AssertionError
|
If options is not a dictionary. |
Source code in superconf/fields.py
FieldString
Bases: Field
A field that stores string values.
Ensures values are stored as strings, converting other types if necessary using Python's built-in str() function.
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to str for automatic type conversion. |
Source code in superconf/fields.py
FieldTuple
Bases: Field
A field that stores tuple values.
Uses the AsTuple cast to convert various inputs to tuples, including: - Comma-separated strings - Other sequence types - Empty values to empty tuples
Attributes:
Name | Type | Description |
---|---|---|
cast |
Set to AsTuple() for automatic type conversion. |