coerce-value-typeadapter->preset-validation#3156
Merged
Merged
Conversation
coerce_value now validates each config value against its field's real
pydantic TypeAdapter (a "TryParse"): attempt the value, return it coerced
on success, and leave it unchanged on ValidationError so the schema pass
reports the real error rather than coercion silently swallowing it.
config_type_index is rebuilt by walking the materialized config schema
(global config + every module's exec'd Config), memoizing one TypeAdapter
per annotation. Nested models are recursed into via _field_submodel, so
modules.<name>.<option> keys fall out of the same walk -- replacing the two
divergent build paths (static BBOTConfig walk + preloaded AST strings).
The single materialized schema is now the source of truth for BOTH
validation and coercion, and coercion sees each field's true declared type
(Literal members, Annotated validators, unions) instead of a flattened set
of base type-names.
Deletes the now-dead helpers and tables:
- accepted_types_from_string / accepted_types_from_annotation
- _COLLECTION_NAMES / _TRUE_WORDS / _FALSE_WORDS
- their __all__ entries
args.py: rename the coerce_value arg accepted -> adapter; update docstrings.
All coercion special cases are preserved:
- numeric YAML value -> str field (ConfigDict(coerce_numbers_to_str=True))
- raw CLI strings kept lossless ("1.10", "0755" stay strings, not floats)
- bool words: true/false/yes/no/on/off/1/0
- list/dict literals parsed for collection-typed fields
- os.PathLike -> str (pydantic won't, so we pre-convert)
- date-shaped strings stay strings (_yaml_scalar guard)
- empty string stays ""
- invalid YAML is non-fatal (kept as the raw string)
Behavior change: field validators now run during coercion, so e.g.
SeverityLiteral upcases "low" -> "LOW". Scoped to baddns* min_severity /
min_confidence; strictly more correct; covered by tests.
Tested (clean HOME): 64/64 test_cli/test_validate_preset/test_presets/
test_config, plus 14/14 test_modules_basic + baddns end-to-end. Index =
832 adapters built in ~82ms, cached thereafter.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
🚀 Performance Benchmark Report
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## preset-validation #3156 +/- ##
=================================================
+ Coverage 90% 90% +1%
=================================================
Files 447 447
Lines 40444 40400 -44
=================================================
- Hits 36294 36258 -36
+ Misses 4150 4142 -8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.