Skip to content

Fix KeyError in fancy_model_dump for pydantic models with extra fields#1689

Merged
nathanjmcdougall merged 4 commits intomainfrom
copilot/fix-pre-commit-hooks-syntax
Apr 1, 2026
Merged

Fix KeyError in fancy_model_dump for pydantic models with extra fields#1689
nathanjmcdougall merged 4 commits intomainfrom
copilot/fix-pre-commit-hooks-syntax

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Pre-commit config files using prek syntax contain extra fields like priority: 0 on hooks and minimum_prek_version at the top level. These are accepted by our pydantic models (extra="allow"), but _fancy_model_dump_base_model crashes because for key, value in model: yields extra fields while model.model_fields[key] only contains declared fields.

Fix

In _fancy_model_dump_base_model, use model_fields.get(key) instead of model_fields[key] and branch:

field_info = model.__class__.model_fields.get(key)
if field_info is not None:
    # declared field: check defaults, aliases, reference diffing
    ...
else:
    # extra field: always include, use raw key
    display_key = key

Tests

  • Unit tests (TestExtraFields): extra field inclusion, interaction with defaults/references, nested models
  • Integration tests: add_repo against YAML with priority, minimum_prek_version, and arbitrary extra keys at top-level/repo/hook levels, verified via parsed YAML structure

Copilot AI and others added 2 commits April 1, 2026 04:27
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>
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
Copilot AI requested a review from nathanjmcdougall April 1, 2026 04:31
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 1, 2026 09:44
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 1, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/fix-pre-commit-hooks-syntax (b9d38c6) with main (1c60f86)

Open in CodSpeed

@nathanjmcdougall nathanjmcdougall merged commit 9cb022d into main Apr 1, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/fix-pre-commit-hooks-syntax branch April 1, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Respect inserting pre-commit hooks into a config file following prek syntax

2 participants