cfx.types.typed_field¶
Annotation-native field factory.
Field() is the primary way to declare config fields when you want the
field type inferred automatically from the type annotation:
class SearchConfig(Config):
n_sigma: float = Field(5.0, "Detection threshold", ge=0.0)
method: Literal["DBSCAN", "RANSAC"] = Field("DBSCAN", "Algorithm")
For custom field types or advanced validation, import explicit types from
cfx.types and declare them directly:
from cfx.types import Float, Options
class SearchConfig(Config):
n_sigma = Float(5.0, "Detection threshold", ge=0.0)
method = Options(("DBSCAN", "RANSAC"), "Algorithm")
Classes¶
Placeholder returned by |
Functions¶
|
Declare an annotation-native config field. |
|
Instantiate the right |
Module Contents¶
- class cfx.types.typed_field.FieldSpec(default, doc, **kwargs)[source]¶
Placeholder returned by
Field(); resolved at class-definition time.Users should not instantiate this directly — use
Field()instead.Config.__init_subclass__replaces everyFieldSpecwith the appropriateConfigFieldsubclass before any instance is created.- default¶
- doc¶
- kwargs¶
- cfx.types.typed_field.Field(default, doc='', **kwargs)[source]¶
Declare an annotation-native config field.
Use as the right-hand side of a type-annotated class attribute on a
Configsubclass. The concreteConfigFieldsubclass is inferred from the annotation at class-definition time:from typing import Literal from cfx import Config, Field class SearchConfig(Config): n_sigma: float = Field(5.0, "Detection threshold", ge=0.0) method: Literal["DBSCAN", "RANSAC"] = Field("DBSCAN", "Algorithm") verbose: bool = Field(False, "Enable verbose output")
choices=is an alternative toLiteral[...]annotations — useful when the allowed values are a runtime constant rather than a literal:ALGORITHMS = ("DBSCAN", "RANSAC", "IsolationForest") class SearchConfig(Config): method: str = Field("DBSCAN", "Algorithm", choices=ALGORITHMS)
Callable defaults are supported for computed fields:
class DerivedConfig(Config): base: float = Field(1.0, "Base value") derived: float = Field(lambda self: self.base * 2, "Derived value")
- Parameters:
- default
objectorcallable Default value or lazy factory accepting the owning instance.
- doc
str, optional Human-readable description shown in display tables.
- choices
tuple, optional Allowed values. Resolves to
Options(orMultiOptionswhen the annotation isset). UnlikeLiteral[...], the choices are not visible to static type checkers — invalid assignments are caught at runtime only.- **kwargs
Any keyword argument accepted by the resolved field type (e.g.
ge=,- ``le=``, ``gt=``, ``lt=``, ``env=``, ``static=``, ``transient=``).
- default
- Returns:
- spec
FieldSpec Placeholder resolved to a
ConfigFieldat class-definition time.
- spec
- cfx.types.typed_field.resolve_field_spec(name, spec, annotation)[source]¶
Instantiate the right
ConfigFieldsubclass for an annotation.Called by
Config.__init_subclass__for everyFieldSpecin the class body. Not part of the end-user API but importable for advanced use.- Parameters:
- Returns:
- field
ConfigField A fully constructed field descriptor.
- field
- Raises:
- TypeError
If the annotation cannot be mapped to a known
ConfigFieldtype.