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.
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.
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. |