Repository navigation
Only clear KeyError from rename mapping lookups - #1408
Merged
provinzkraut merged 1 commit intoDec 3, 2026
Merged
Conversation
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.
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.
Fixes #1407
When
rename=was a mapping, any exception raised by its__getitem__was cleared and the field kept its own name. Now onlyKeyErrormeans the field is missing. Other exceptions propagate and fail class creation, as they already do for a callablerename=.This is a behaviour change. A custom
rename=mapping that raised a non-KeyErrorexception for a missing name used to be silently ignored, and now the class definition fails with that exception. Dicts and mappings that raiseKeyErrorfor missing keys are unaffected. If maintainers would rather treatLookupErroras "missing", that is a small follow-up.Change:
rename_mappinginsrc/msgspec/_core.creturnsNULLfor non-KeyErrorexceptions instead of clearing them. A regression test,TestRename::test_rename_mapping_getitem_error_propagates, is added totests/unit/test_struct.py.How it was tested:
test_struct.py,test_struct_meta.py,test_convert.pyandtest_cpylint.py: 1283 passed, 38 skipped.just checkwas not run.