Skip to content

Fix Field.error_messages type to allow dict and list values#2907

Merged
sloria merged 4 commits intomarshmallow-code:devfrom
dhruvildarji:fix/error-messages-typing
Mar 25, 2026
Merged

Fix Field.error_messages type to allow dict and list values#2907
sloria merged 4 commits intomarshmallow-code:devfrom
dhruvildarji:fix/error-messages-typing

Conversation

@dhruvildarji
Copy link
Copy Markdown
Contributor

Summary

Closes #1636.

The Field.error_messages parameter was typed as dict[str, str], but error message values can be dicts or lists (e.g., the existing test test_required_message_can_be_changed already passes dict and list values). The make_error method also already handles non-string values correctly.

Changes

  • Added ErrorMessageValue and ErrorMessages type aliases to types.py
  • Updated 4 type annotations in fields.py from dict[str, str] to types.ErrorMessages:
    • _BaseFieldKwargs.error_messages
    • Field.default_error_messages
    • Field.__init__ parameter
    • Local messages variable in __init__

Test plan

  • All 1132 existing tests pass (no test changes needed)
  • Existing test test_required_message_can_be_changed already validates dict and list error message values

dhruvildarji and others added 4 commits February 25, 2026 10:48
The error_messages parameter on Field and its type annotation were
typed as dict[str, str], but error messages can actually be dicts or
lists (as used in tests and supported by ValidationError). This updates
the type to dict[str, str | list | dict] via a new ErrorMessages type
alias, fixing marshmallow-code#1636.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sloria sloria merged commit 3bc191a into marshmallow-code:dev Mar 25, 2026
9 checks passed
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.

Field.error_messages should allow dict and list values

2 participants