tests.test_config_schema module¶
Tests for typed configuration schemas, validation, and extension points.
- tests.test_config_schema.test_normalize_config_keys_keeps_a_key_that_is_not_a_string()[source]¶
YAML reads
1:as a number: such a key has no hyphen to replace.
- tests.test_config_schema.test_config_schema_dataclass(invoke, create_config)[source]¶
Dataclass schemas are auto-detected and instantiated with normalized keys.
- tests.test_config_schema.test_config_schema_cascade(invoke, tmp_path, monkeypatch)[source]¶
With cascade=True, the schema is built from the merged view.
- tests.test_config_schema.test_config_schema_callable(invoke, create_config)[source]¶
A plain callable can be used as config_schema.
- tests.test_config_schema.test_config_schema_no_config_file(invoke)[source]¶
When no config file is found, schema defaults are used.
- tests.test_config_schema.test_config_schema_dataclass_defaults(invoke, create_config)[source]¶
Dataclass defaults are used for fields not present in the config file.
- tests.test_config_schema.test_fallback_sections(invoke, create_config)[source]¶
Legacy section names are recognized with a deprecation warning.
- tests.test_config_schema.test_fallback_section_warns_once(invoke, create_config)[source]¶
A legacy section loaded with its themes is reported once, not per reader.
- tests.test_config_schema.test_fallback_sections_prefers_current(invoke, create_config)[source]¶
When both current and legacy sections exist, current wins.
- tests.test_config_schema.test_config_schema_multiple_formats(invoke, create_config, conf_name, conf_text)[source]¶
Config schema works with YAML and JSON, not just TOML.
- tests.test_config_schema.test_config_schema_on_config_option_directly(invoke, create_config)[source]¶
Config schema can be set directly on ConfigOption via the decorator.
- tests.test_config_schema.test_get_tool_config_defaults_to_current_context(invoke, create_config)[source]¶
get_tool_config() works without passing ctx explicitly.
- tests.test_config_schema.test_flatten_config_keys_with_normalize()[source]¶
flatten + normalize maps nested kebab-case config to flat snake_case fields.
- tests.test_config_schema.test_config_schema_nested_toml(invoke, create_config)[source]¶
Nested TOML sub-tables map to flat dataclass fields via flattening.
- tests.test_config_schema.test_config_schema_strict_rejects_unknown(invoke, create_config)[source]¶
schema_strict=True raises ValueError on unrecognized config keys.
- tests.test_config_schema.test_config_schema_strict_passes_when_valid(invoke, create_config)[source]¶
schema_strict=True does not raise when all config keys are known.
- tests.test_config_schema.test_config_schema_strict_with_nested(invoke, create_config)[source]¶
schema_strict=True validates flattened keys from nested sub-tables.
- tests.test_config_schema.test_config_schema_lax_warns_unknown_when_schema_only(invoke, create_config)[source]¶
A schema-only section warns on unknown keys instead of dropping them silently.
included_params=()means no CLI parameter is merged from the app’s section, so any key the schema does not know can only be a typo: lax mode then logs a warning while still loading the known fields.
- tests.test_config_schema.SCHEMA_ONLY_CONF = '[schema-only-cli]\nknown-field = "good"\ntypo-field = "oops"\n'¶
A schema-only section, holding one key its schema does not know.
- tests.test_config_schema.test_schema_only_section_loads_in_strict_mode(invoke, create_config)[source]¶
The CLI-parameter strict check leaves a schema-only section to the schema.
No parameter is read from it, so the schema alone reports the key it does not know, which lax mode only warns about.
- tests.test_config_schema.test_validate_config_warns_unknown_when_schema_only(invoke, create_config)[source]¶
--validate-configaccepts a schema-only section, naming its unknown key.The file stays valid, as it loads: lax mode only warns.
- tests.test_config_schema.test_config_schema_lax_silent_when_params_merged(invoke, create_config)[source]¶
Without
included_params=(), lax mode stays silent on unknown keys.The section may legitimately mix CLI parameter keys with schema fields, so a key unknown to the schema is not necessarily a typo.
- class tests.test_config_schema.MixedConfig(extra_stuff='', nested=MixedNestedConfig(), sub_section_key=0)[source]¶
Bases:
object- nested: MixedNestedConfig¶
- tests.test_config_schema.MIXED_CONF = '[mixed-cli]\nfruit = "plum"\nextra-stuff = "x"\n\n[mixed-cli.nested]\ndepth = 3\n\n[mixed-cli.sub-section]\nkey = 2\n'¶
A section mixing the
--fruitparameter with the fields ofMixedConfig.Each field takes a shape a schema reads: a scalar, a nested dataclass table, and a table flattened into a field name.
- tests.test_config_schema.test_strict_check_leaves_schema_keys_to_the_schema(invoke, create_config, kwargs, args, expected)[source]¶
The CLI-parameter strict check accepts the keys the dataclass schema reads.
--validate-configalways runs the check, andconfig_strict=Trueruns it as the file loads.
- tests.test_config_schema.test_strict_check_rejects_a_key_neither_reads(invoke, create_config)[source]¶
A key matching neither a parameter nor a schema field stays an error.
- class tests.test_config_schema.PinnedConfig(extra_stuff='', nested=MixedNestedConfig(), sub_section_key=0, pinned=dict(), axes=dict())[source]¶
Bases:
object- nested: MixedNestedConfig¶
- tests.test_config_schema.test_schema_keys(section, expected)[source]¶
_schema_keysreturns the keys of a section the dataclass reads in full.
- tests.test_config_schema.test_schema_keys_of_a_callable_schema()[source]¶
A callable schema does not tell which keys it reads.
- tests.test_config_schema.test_make_schema_callable_warn_unknown(caplog)[source]¶
warn_unknown=Truelogs unknown keys, recursing into nested dataclasses.
- tests.test_config_schema.test_make_schema_callable_lax_default_is_silent(caplog)[source]¶
Without
warn_unknown, lax mode keeps dropping unknown keys silently.
- tests.test_config_schema.test_pyproject_toml_cwd_discovery(invoke, tmp_path, monkeypatch)[source]¶
pyproject.toml in CWD is discovered automatically without –config.
- tests.test_config_schema.test_pyproject_toml_cwd_discovery_walks_up(invoke, tmp_path, monkeypatch)[source]¶
pyproject.toml discovery walks up from CWD to parent directories.
- tests.test_config_schema.test_pyproject_toml_explicit_config_skips_cwd(invoke, create_config, tmp_path, monkeypatch)[source]¶
Explicit –config skips CWD pyproject.toml discovery.
A pyproject.toml without [tool.<cli_name>] is skipped.
Regression: a pyproject.toml carrying only unrelated [tool.X] sections (like a dotfiles repo’s [tool.ruff]) used to shadow the user’s app-dir config. It must now be ignored so the CLI falls back to its defaults instead of inheriting an unrelated project’s settings.
CWD walk continues past a pyproject.toml lacking [tool.<cli_name>].
A nearer pyproject.toml that only carries unrelated [tool.X] sections must not stop the upward walk: a parent pyproject.toml with a matching [tool.<cli_name>] section should still be discovered.
- tests.test_config_schema.test_pyproject_toml_cwd_mixed_tool_sections(invoke, tmp_path, monkeypatch)[source]¶
[tool.<cli_name>] is picked from a pyproject.toml that also has others.
An unrelated pyproject.toml falls through to the app-dir config.
Directly exercises the documented intent of the fix: when CWD contains a pyproject.toml whose only [tool.X] sections are unrelated to the CLI, the walk must give up so the standard app-dir search can find the user’s actual config and apply it.
- tests.test_config_schema.test_flatten_config_keys_opaque()[source]¶
opaque_keys stops flattening at matching key boundaries.
- tests.test_config_schema.test_flatten_config_keys_opaque_nested()[source]¶
opaque_keys works at deeper nesting levels.
- tests.test_config_schema.test_schema_type_aware_flattening(invoke, create_config)[source]¶
dict-typed dataclass fields stop flattening automatically.
- tests.test_config_schema.test_schema_field_metadata_config_path(invoke, create_config)[source]¶
click_extra.config_path extracts a value at a dotted TOML path.
- tests.test_config_schema.test_schema_field_metadata_normalize_keys_true(invoke, create_config)[source]¶
click_extra.normalize_keys defaults to True: keys are normalized.
- tests.test_config_schema.test_schema_nested_dataclass(invoke, create_config)[source]¶
Nested dataclass fields are recursively instantiated.
- tests.test_config_schema.test_schema_nested_dataclass_with_opaque_fields(invoke, create_config)[source]¶
Nested dataclass with dict-typed fields preserves opaque keys.
- tests.test_config_schema.test_schema_nested_dataclass_defaults(invoke, create_config)[source]¶
Nested dataclass uses defaults when config section is absent.
- tests.test_config_schema.test_strict_skips_opaque_dict_field(invoke, create_config)[source]¶
Strict mode does not reject keys inside a
dict[str, X]schema field.A field typed as
dict[str, dict]is user-controlled: the keys are data, not CLI flag names. Click-extra strips that sub-tree before running its unknown-key check so app extensions don’t trip strict mode.
- tests.test_config_schema.test_strict_skips_opaque_metadata_field(invoke, create_config)[source]¶
Strict mode also honors the
EXTENSION_METADATA_KEYmarker on a field whose Python type is not a mapping.
- tests.test_config_schema.test_validate_config_skips_opaque_field(invoke, create_config)[source]¶
–validate-config also skips opaque sub-trees, so a config that the runtime accepts also passes validation.
- tests.test_config_schema.test_opaque_field_keeps_dotted_keys(invoke, create_config, caplog, strict)[source]¶
A dot in a key of an opaque sub-tree is part of the key, not a path.
Dotted-key expansion skips extension points, whose keys are data. A key with a trailing, inner or doubled dot reaches the schema whole, logs no empty-segment warning and passes strict mode.
- tests.test_config_schema.test_config_validator_runs_and_fails_under_validate_config(invoke, create_config)[source]¶
A registered
ConfigValidatorruns during--validate-configand surfaces itsValidationErrorwith a path rooted at the config file.
- tests.test_config_schema.test_config_validator_runs_during_normal_load(invoke, create_config)[source]¶
A misconfigured opaque sub-tree fails fast during normal config loading, not only under
--validate-config.
- tests.test_config_schema.test_config_validator_extension_path_strips_strict_check(invoke, create_config)[source]¶
A ConfigValidator(extension_path=…) registration alone is enough to skip strict-check on that path, even when the schema doesn’t have the field.
- tests.test_config_schema.test_config_validator_collects_all_errors(invoke, create_config)[source]¶
--validate-configreports every detected error in one pass.A config with both an unknown CLI flag key and a validator-flagged field should surface both messages before the run exits non-zero, so the user sees the full punch list.
- tests.test_config_schema.test_collect_opaque_paths_from_schema()[source]¶
Schema introspection picks up dict-typed fields, metadata-marked fields, and nested-dataclass opaque fields with dotted prefixes.
- tests.test_config_schema.test_schema_strict_honors_extension_metadata_on_non_mapping_field(invoke, create_config)[source]¶
schema_strict must not descend into an EXTENSION_METADATA_KEY-marked field whose Python type is not a mapping.
Before the opaque-path unification, the outer strip honored the marker but the dataclass adapter’s own flatten boundary inspected only the type hint, so the marked sub-tree was flattened into dotted keys and rejected as unknown.
- tests.test_config_schema.test_run_config_validation_valid_document()[source]¶
A clean document yields an ok report with the schema instance built and every opaque sub-tree extracted.
- tests.test_config_schema.test_run_config_validation_exposes_merged_conf()[source]¶
A passing strict check carries the template-filtered config as merged_conf, with recognized values merged in and unknown keys dropped.
- tests.test_config_schema.test_run_config_validation_collects_all_then_short_circuits()[source]¶
collect_all=True gathers errors from every stage in order; collect_all=False stops after the first.
- tests.test_config_schema.test_run_config_validation_wraps_schema_errors()[source]¶
A schema_strict failure is recorded as a ValidationError with the schema_error code, and the message is preserved verbatim.
- tests.test_config_schema.test_run_config_validation_no_schema_no_template()[source]¶
With neither a template nor a schema, the report is ok and carries no schema instance.
- tests.test_config_schema.test_select_app_section_of_an_empty_app_name_is_the_document()[source]¶
An empty app name reads the whole document as the section.
_rooted_pathand_scope_app_sectionsalready read it that way.
- tests.test_config_schema.test_run_config_validation_of_an_empty_app_name()[source]¶
An empty app name feeds the whole document to the schema and validators.
- tests.test_config_schema.test_run_config_validation_reports_a_key_that_is_not_a_string()[source]¶
A key YAML reads as a number is an invalid key to the strict check, and an unknown option to a strict schema, never a crash.
- tests.test_config_schema.test_make_schema_callable_coerces_dict_to_dataclass()[source]¶
The public make_schema_callable turns a raw config dict into a dataclass.
- tests.test_config_schema.test_field_docstrings_returns_full_text()[source]¶
Attribute docstrings are recovered whole, paragraph breaks preserved.
- tests.test_config_schema.test_field_docstrings_degrades_without_source()[source]¶
A class defined through exec has no source: the mapping is empty.
- tests.test_config_schema.test_schema_field_infos_flat_schema()[source]¶
Keys are kebab-cased and sorted; defaults, types, and summaries surface.
- tests.test_config_schema.test_schema_field_infos_nested_and_config_path()[source]¶
Nested dataclasses expand to dotted keys; config_path metadata wins.
- tests.test_config_schema.test_schema_field_infos_sorts_segment_wise()[source]¶
A sub-table’s options stay contiguous when a sibling shares their prefix.
Plain string sort would interleave
pear-cellarbetweenpear.cratesandpear.pickers(in ASCII-sorts before.); segment-wise sort keeps thepeartable’s options together.