elastic / elastic/ecs-logging-python

StructlogProcessor fails typechecking

Open
#146 2 comments 0 reactions 0 assignees View on GitHub
agent-python community triage
Dominant language
Python
Stars
74
Forks
33
Avg merge
9h 3m
Merged PRs (30d)
1

Description

When typechecking the following code:

```python
from structlog.typing import Processor

shared_processors: tuple[Processor, ...] = (
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.set_exc_info,
structlog.processors.TimeStamper(fmt="iso", utc=True),
)

processors: list[Processor]
if sys.stderr.isatty():
processors = [
*shared_processors,
structlog.dev.ConsoleRenderer(),
]
else:
processors = [
*shared_processors,
structlog.processors.dict_tracebacks,
ecs_logging.StructlogFormatter(),
]

structlog.configure(
processors=processors,
logger_factory=structlog.PrintLoggerFactory(),
cache_logger_on_first_use=True,
)
```

mypy raises the following:

```
error: List item 2 has incompatible type "StructlogFormatter"; expected
"Callable[[Any, str, MutableMapping[str, Any]], Union[Mapping[str, Any], str, bytes, bytearray, tuple[Any, ...]]]" [list-item]
ecs_logging.StructlogFormatter(),
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: "StructlogFormatter.__call__" has type "Callable[[Arg(Any, '_'), Arg(str, 'name'), Arg(dict[str, Any], 'event_dict')], str]"
```

`StructlogFormatter` is annotated as accepting a `dict[str, Any]`, but it needs to accept any `MutableMapping[str, Any]`. The code already conforms to this, so addressing this issue wil hopefully only involve updating the type annotation.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.