cfx.types.config_field¶
Base descriptor class for self-documenting configuration fields.
This module defines ConfigField, which implements the Python descriptor
protocol to provide validated, documented configuration fields that live
directly on Config class definitions. Subclass ConfigField and override
ConfigField.validate to add type or value constraints.
Classes¶
Base descriptor for a single configuration field. |
Module Contents¶
- class cfx.types.config_field.ConfigField(default_value, doc, static=False, env=None, transient=None)[source]¶
Bases:
Generic[T]Base descriptor for a single configuration field.
Implements
__get__,__set__, and__set_name__so that instances assigned as class attributes behave as managed, validated attributes onConfigsubclasses.The default value may be a plain object or a callable that accepts the owning instance as its sole argument (a lazy factory). When a lazy factory is provided, the computed value is returned on every access until the user explicitly sets the attribute, after which the stored value is used instead.
- Parameters:
- default_value
objectorcallable Default value for the field. If callable, it is treated as a lazy factory:
default_value(instance)is called on each__get__invocation until an explicit value is stored on the instance.- doc
str Human-readable description shown in
__str__tables and Jupyter_repr_html_output.- static
bool, optional If
Truethe value is frozen todefault_valueat class-definition time and cannot be changed on instances. Any attempted__set__raisesAttributeError. Default isFalse.- env
strorNone, optional Name of an environment variable to consult when no explicit value has been stored on the instance. When set, the priority chain is: explicit assignment > environment variable > default. The raw string from
os.environis coerced by_from_env_strand then validated. The env value is not stored on the instance - it is re-read on every access, matching the behaviour of callable defaults.Nonemeans no environment variable is consulted. Default isNone.
- default_value
- Raises:
Examples
>>> from cfx import Config >>> from cfx.types import ConfigField >>> class MyConfig(Config): ... name = ConfigField("default", "A simple untyped field") >>> cfg = MyConfig() >>> cfg.name 'default' >>> cfg.name = "something else" >>> cfg.name 'something else'
- doc¶
- static = False¶
- env = None¶
- property transient¶
True when the field is skipped on serialization if no value stored.
- normalize(value)[source]¶
Transform value to canonical form before validation and storage.
Called before
validatein both__init__(for non-callable defaults) and__set__. Must be idempotent: applying normalize twice yields the same result as applying it once.Override in subclasses to coerce common input types to the field’s canonical type. Examples:
str -> pathlib.Path,list -> tuple, angular wrap-around to[0, 360).The default implementation returns the value unchanged.
- Parameters:
- valueobject
The raw value to normalize.
- Returns:
- object
The normalized value, ready to pass to
validate.
- validate(value)[source]¶
Validate a value against this field’s constraints.
Called after
normalize. Override in subclasses to enforce correct type or range of values. RaiseTypeErrorfor wrong type, andValueErrorfor out-of-range error. The base implementation accepts any value.- Parameters:
- value
object The normalized value to validate.
- value
- Raises:
- TypeError
If the value has the wrong type.
- ValueError
If the value is outside the allowed range or set.
- from_string(s)[source]¶
Parse a raw string into this field’s type.
Override in subclasses to coerce the raw string to the appropriate type for a particular
ConfigField. The default implementation returns the unchanged string.Called by
__get__whenenvis set and the variable is present in the environment but the instance has no explicitly stored value, and by the CLI integration as the argparsetype=callable. The result is then passed tovalidate.
- to_string(value)[source]¶
Format a field value as a human-readable string for display.
Called by the display layer when rendering the config table. Override in subclasses to produce cleaner output than Python’s default
str(). The default implementation returnsstr(value).
- serialize(value)[source]¶
Serialize value to a JSON/YAML-safe form for
to_dict().Override in subclasses for types that need a different on-disk representation (e.g.
pathlib.Path -> str,set -> sorted list,datetime.time -> ISO string). The default returns the value unchanged.- Parameters:
- valueobject
The current field value (already normalized/canonical).
- Returns:
- object
A JSON/YAML-safe value.
- deserialize(value)[source]¶
Deserialize a value from dict form back to the field’s type.
Logical inverse of
serialize. Called byfrom_dict()beforesetattr; the result is then passed throughnormalizeandvalidatevia__set__. The default returns the value unchanged.- Parameters:
- valueobject
The value from the dict.
- Returns:
- object
The deserialized value, ready to be set on the instance.
- is_set(obj)[source]¶
Return
Trueif an explicit value is stored on obj.Returns
Falsewhen the field is still using its default, env var, or callable factory.- Parameters:
- objConfig
Instance to check.
- unset(obj)[source]¶
Remove the stored value, reverting to default/env/callable.
After calling this,
__get__will re-evaluate the default, environment variable, or callable factory on the next access.- Parameters:
- objConfig
Instance to modify.
- reset(obj, value=None)[source]¶
Reset to default or set a new value with full normalize/validate.
If value is
None, equivalent tounset. Otherwise, sets a new value going through the full normalize -> validate pipeline.- Parameters:
- objConfig
Instance to modify.
- valueobject, optional
New value. If
None, reverts to default.
- to_argparse_kwargs(name, prefix='')[source]¶
Return kwargs dict for
parser.add_argument().The returned dict always contains a
"flag"key with the full option string (e.g."--search.n-sigma"). Pull it out before calling argparse:kwargs = descriptor.to_argparse_kwargs(name, prefix) flag = kwargs.pop("flag") parser.add_argument(flag, **kwargs)
Override in subclasses to customize the argparse representation (add
"choices","nargs","action","metavar").- Parameters:
- namestr
Field attribute name.
- prefixstr, optional
Dot-separated prefix for nested configs (e.g.
"search").
- Returns:
- dict
- to_click_option(name, prefix='')[source]¶
Return a
click.option()decorator for this field.Override in subclasses to customize the click representation.
- Parameters:
- namestr
Field attribute name.
- prefixstr, optional
Dot-separated prefix for nested configs.
- Returns:
- decorator
A
click.option(...)decorator ready to stack on a command.
- Raises:
- ImportError
If
clickis not installed.
- __set_name__(owner, name)[source]¶
Record the attribute name assigned by the owning class.
Called automatically by the metaclass machinery when the descriptor is assigned as a class attribute. Sets
public_name(the name as written in the class body) andprivate_name(the name used to store the per-instance value ininstance.__dict__).
- __get__(obj, objtype)[source]¶
- __get__(obj: object, objtype: type) T
Return the field value for the owning instance.
If accessed on the class (
objisNone) returns aFieldRefpath proxy, enabling IDE-refactorable paths for use inAliasandMirrordeclarations.For static fields, always returns
defaultval.For non-static fields with no explicitly stored value, the lookup order is:
explicit assignment (stored on the instance)
environment variable, when
envis set and the variable is present inos.environdefaultval, called if callable, returned directly otherwise