logging: stop warning_once from crashing on unhashable kwargs like extra={...}#4047
Open
c-tonneslan wants to merge 1 commit into
Open
logging: stop warning_once from crashing on unhashable kwargs like extra={...}#4047c-tonneslan wants to merge 1 commit into
c-tonneslan wants to merge 1 commit into
Conversation
`MultiProcessAdapter.warning_once` was decorated with
`@functools.lru_cache(None)`, which hashes every positional and
keyword argument. The standard `logging` API accepts an `extra={...}`
kwarg, and a dict isn't hashable, so a perfectly normal call like
logger.warning_once("only once", extra={"id": 1})
raised `TypeError: unhashable type: 'dict'`.
Cache by the message text on a per-adapter set instead. That matches
the docstring (`"the same message only once"`), accepts any kwargs the
underlying `warning()` accepts, and also drops the implicit `self`
retention that `lru_cache` on a method caused.
Added a regression test in `tests/test_logging.py` that calls
`warning_once` with an `extra={...}` kwarg, twice with the same
message and once with a different message, and asserts each unique
message is emitted exactly once.
Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.
`MultiProcessAdapter.warning_once` is decorated with `@functools.lru_cache(None)`, which hashes every positional and keyword argument. The standard `logging` API accepts an `extra={...}` kwarg, but dicts aren't hashable, so a normal call like
```python
logger = get_logger(name)
logger.warning_once("only once", extra={"id": 1})
```
raises `TypeError: unhashable type: 'dict'`. Same story for any other unhashable kwarg (`stack_info=False` is fine, but `extra` is the common one).
Cache by the message text on a per-adapter set instead. That matches the docstring ("the same message only once"), accepts whatever kwargs the underlying `warning()` accepts, and as a bonus drops the implicit `self` retention that an `lru_cache(None)` on an instance method causes.
Added a regression test in `tests/test_logging.py` that calls `warning_once` with an `extra={...}` kwarg, twice with the same message and once with a different one, and asserts each unique message is emitted exactly once. The two existing logging tests still pass.
Sample crash before:
```pycon