Skip to content

Only clear KeyError from rename mapping lookups - #1408

Merged
provinzkraut merged 1 commit into
msgspec:mainfrom
shashank11yadav:fix/rename-mapping-getitem-errors
Dec 3, 2026
Merged

provinzkraut merged 1 commit into
msgspec:mainfrom
shashank11yadav:fix/rename-mapping-getitem-errors

Conversation

@shashank11yadav

Copy link
Copy Markdown
Contributor

Fixes #1407

When rename= was a mapping, any exception raised by its __getitem__ was cleared and the field kept its own name. Now only KeyError means the field is missing. Other exceptions propagate and fail class creation, as they already do for a callable rename=.

This is a behaviour change. A custom rename= mapping that raised a non-KeyError exception for a missing name used to be silently ignored, and now the class definition fails with that exception. Dicts and mappings that raise KeyError for missing keys are unaffected. If maintainers would rather treat LookupError as "missing", that is a small follow-up.

Change: rename_mapping in src/msgspec/_core.c returns NULL for non-KeyError exceptions instead of clearing them. A regression test, TestRename::test_rename_mapping_getitem_error_propagates, is added to tests/unit/test_struct.py.

How it was tested:

  • The new test fails without the fix and passes with it.
  • The unit tests for test_struct.py, test_struct_meta.py, test_convert.py and test_cpylint.py: 1283 passed, 38 skipped.
  • ruff check, ruff format --diff and codespell pass, run directly. just check was not run.

When rename= is a mapping, any exception raised by __getitem__ was
cleared and the field kept its own name. Only KeyError now means the
field is missing; other exceptions propagate and fail class creation,
as they already do for a callable rename.

@provinzkraut provinzkraut left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@provinzkraut
provinzkraut merged commit 5d8131a into msgspec:main Dec 3, 2026
23 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.

Struct rename= mapping silently swallows non-KeyError exceptions from __getitem__

2 participants