Passing complex CLI params to Pydantic settings
Settings Management - Pydantic Validation is nice but doesn’t cover everything.
Passing None through CLI
- Settings Management - Pydantic Validation
Noneby default b/ccli_avoid_jsonis the default,[]
it will default to “null” if
cli_avoid_jsonisFalse, and “None” ifcli_avoid_jsonisTrue.
Fun fact, works inside complex nested JSON flags as well.
Passing complex JSON-like fields incl. tuples
# Assume this
class MySettingsClass(BaseSettings):
I_am_a_list_of_tuples: list[tuple[str, str, str | None]] = Field(
default=FILE_PAIRS, validation_alias=AliasChoices("fp", "file_pairs")
)
FILE_PAIRS = [
(
"infer_doc_simplecriteria/pred_doc_simplecriteria.json",
"merge_gold_simplecriteria/gold_simplecriteria.json",
),
(
"file2-1",
"file2-2",
),
]
# Passing as a list of lists parses as a tuple
# if done wrong the tuple is parsed as a list of strings
uv run thing --I-am-a-list-of-tuples \
'"merge_gold_simplecriteria/gold_simplecriteria.json", "infer_doc_simplecriteria/pred_doc_simplecriteria.json"'
Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus