Fix KeyError in fancy_model_dump for pydantic models with extra fields#1689
Merged
nathanjmcdougall merged 4 commits intomainfrom Apr 1, 2026
Merged
Fix KeyError in fancy_model_dump for pydantic models with extra fields#1689nathanjmcdougall merged 4 commits intomainfrom
nathanjmcdougall merged 4 commits intomainfrom
Conversation
Handle extra fields from pydantic models with extra="allow" (such as `priority` from prek syntax) in `_fancy_model_dump_base_model`. Previously, iterating over such models included extra fields, but accessing `model_fields[key]` raised KeyError since extras aren't in model_fields. Now extra fields are always included in the dump using the raw key, preserving them through the round-trip. Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/62f13138-115b-4f5d-a26d-16bafb46ff24 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/62f13138-115b-4f5d-a26d-16bafb46ff24 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix inserting pre-commit hooks into config file
Fix KeyError in fancy_model_dump for pydantic models with extra fields
Apr 1, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Apr 1, 2026
nathanjmcdougall
approved these changes
Apr 1, 2026
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.
Pre-commit config files using prek syntax contain extra fields like
priority: 0on hooks andminimum_prek_versionat the top level. These are accepted by our pydantic models (extra="allow"), but_fancy_model_dump_base_modelcrashes becausefor key, value in model:yields extra fields whilemodel.model_fields[key]only contains declared fields.Fix
In
_fancy_model_dump_base_model, usemodel_fields.get(key)instead ofmodel_fields[key]and branch:Tests
TestExtraFields): extra field inclusion, interaction with defaults/references, nested modelsadd_repoagainst YAML withpriority,minimum_prek_version, and arbitrary extra keys at top-level/repo/hook levels, verified via parsed YAML structure