pydantic_click_options
pydantic_click_options
¶
Generate Click CLI options from a Pydantic model.
Used by cli/run.py and cli/config.py to expose every
SafeSynthesizerParameters field as a --field_name CLI option.
Nested BaseModel fields are flattened with a separator
(e.g. --data__holdout). Fields typed as SomeModel | None also
get a --no-<field> is-flag that sets the field to None.
The companion parse_overrides() reverses the flattening at runtime,
converting Click's flat {key: value} dict back into the nested structure
Pydantic expects. The field_sep argument to parse_overrides must
match the field_separator passed to pydantic_options; otherwise
nested keys like data__holdout will not be reconstructed correctly.
Classes:
| Name | Description |
|---|---|
AutoParamType |
A Click type that accepts the sentinel |
Functions:
| Name | Description |
|---|---|
parse_overrides |
Parse Click kwargs into a nested override dict. |
pydantic_options |
Decorate a Click command with options derived from a Pydantic model. |
LeafParam(name, field)
dataclass
¶
A scalar CLI option backed by a Pydantic FieldInfo.
FlagParam(name, field_name)
dataclass
¶
A --no-<field> is-flag that sets the named field to None.
AutoParamType(base_type)
¶
Bases: ParamType
A Click type that accepts the sentinel AUTO_STR or a base numeric/bool value.
Used for Auto*Param fields (AutoIntParam, AutoFloatParam,
AutoBoolParam) so that --flag auto and --flag 2 both work.
The --help display shows integer|auto, float|auto, or
boolean|auto instead of the generic TEXT label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_type
|
ParamType
|
The underlying Click type ( |
required |
Methods:
| Name | Description |
|---|---|
convert |
Convert the raw CLI value to |
Source code in src/nemo_safe_synthesizer/configurator/pydantic_click_options.py
convert(value, param, ctx)
¶
Convert the raw CLI value to AUTO_STR or the base numeric/bool type.
The value parameter is typed str to match the parent
click.ParamType.convert stub signature; in practice Click may also
pass through default values of any type, but the equality check and
delegated base_type.convert both handle that correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Raw value from the CLI or the option default. |
required |
param
|
Parameter | None
|
The Click parameter object (passed through to the base type). |
required |
ctx
|
Context | None
|
The Click context (passed through to the base type). |
required |
Returns:
| Type | Description |
|---|---|
str | int | float | bool
|
|
str | int | float | bool
|
delegating to |
Source code in src/nemo_safe_synthesizer/configurator/pydantic_click_options.py
parse_overrides(values=None, field_sep='__')
¶
Parse Click kwargs into a nested override dict.
no_<field>=True injects {field: None} to disable a nullable-model
field. no_<field>=False (unset is-flag) is silently dropped.
None values (unset regular options) are also dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
dict[str, Any] | None
|
Flat dictionary of command line arguments from Click. ( |
None
|
field_sep
|
str
|
Separator used to reconstruct nesting. For example, |
'__'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A nested dict suitable for |
dict[str, Any]
|
with a loaded config via |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a key contains empty segments (e.g. consecutive
separators like |
Source code in src/nemo_safe_synthesizer/configurator/pydantic_click_options.py
pydantic_options(model_class, field_separator='__')
¶
Decorate a Click command with options derived from a Pydantic model.
Recurses into nested sub-models, flattening their fields into top-level
CLI options separated by field_separator. Fields typed as
SomeModel | None also get a --no-<field> is-flag that sets the
field to None when passed. Field types are mapped to Click types
via _CLICK_TYPE_PRIORITY; help text is pulled from
Field(description=...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
type[BaseModel]
|
The Pydantic model to generate options from
(typically |
required |
field_separator
|
str
|
String used to join parent and child field names
in the CLI option (default |
'__'
|
Returns:
| Type | Description |
|---|---|
|
A Click decorator that attaches the generated options to a command. |