Skip to content

Commit

Permalink
Fix 3.8 typing error and add test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Dickinson committed Nov 10, 2023
1 parent 8d44554 commit 02327f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import warnings

from functools import partial
from typing import Any, Callable, Collection, Iterable, Sequence, cast
from typing import Any, Callable, Collection, Dict, Iterable, Sequence, cast

from . import _config
from ._base import BoundLoggerBase
Expand Down Expand Up @@ -1036,7 +1036,7 @@ def format(self, record: logging.LogRecord) -> str:
# We need to copy because it's possible that the same record gets
# processed by multiple logging formatters. LogRecord.getMessage
# would transform our dict into a str.
ed = cast(dict[str, Any], record.msg).copy()
ed = cast(Dict[str, Any], record.msg).copy()
ed["_record"] = record
ed["_from_structlog"] = True
else:
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def format(self, record: logging.LogRecord) -> str:

if not isinstance(ed, str):
warnings.warn(
"The last processor in ProcessorFormatter.formatters must "
"The last processor in ProcessorFormatter.processors must "
f"return a string, but {self.processors[-1]} returned a "
f"{type(ed)} instead.",
category=RuntimeWarning,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,25 @@ def test_remove_processors_meta(self):
{"foo": "bar", "_record": "foo", "_from_structlog": True},
)

def test_non_string_message_warning(self):
"""
A warning is raised if the last processor in
ProcessorFormatter.processors doesn't return a string.
"""
configure_logging(None)
logger = logging.getLogger()

formatter = ProcessorFormatter(
processors=[lambda *args, **kwargs: {"foo": "bar"}],
)
logger.handlers[0].setFormatter(formatter)

with pytest.warns(
RuntimeWarning,
match="The last processor in ProcessorFormatter.processors must return a string",
):
logger.info("baz")


@pytest_asyncio.fixture(name="abl")
async def _abl(cl):
Expand Down

0 comments on commit 02327f8

Please sign in to comment.