Conversation
Python 3.8+ logging { and $ styles raise ValueError instead of
KeyError, so fill_missing_fmt_key=True never filled those keys.
Signed-off-by: Christopher Pruijsen <christopher.pruijsen@gmail.com>
cpruijsen
force-pushed
the
fix/issue-189
branch
from
September 11, 2026 13:57
4434133 to
23ba28b
Compare
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.
Catch
(KeyError, ValueError)in_format_by_dictsoFluentRecordFormatter(fill_missing_fmt_key=True)fills missing{and$style fields withNoneon Python 3.8+. Since Python 3.8,logging.StrFormatStyle/StringTemplateStylewrap a missing field asValueErrorinstead ofKeyError(python/cpython#79025)._format_by_dictonly caughtKeyError, so the fill flag did nothing for those styles.%style was already handled because it still usesvalue % record.__dict__.Fixes #189
Decision
Catch
(KeyError, ValueError)in the existing handler, as asked in the issue ("ValueError must be handled here"). The other option is to treatValueErroras a missing key only when__context__isKeyError, so an invalid format spec (e.g.{name:d}on a str) still raises whenfill_missing_fmt_key=True._format_by_dictalready uses a bare type catch; the flag is opt-in; inspecting__context__is more clever than the rest ofhandler.py. Can switch to the narrower match.Test plan
pytest tests/test_handler.py(20 passed, including new{/$fill tests)_format_by_dict(ValueError: Formatting field not found in record: 'custom_field'), each in isolationpytest tests/test_asynchandler.py(15 passed; formatter is shared)KeyErrorfor{/$; both types are caught)