elastic / elastic/ecs-logging-python

StructlogProcessor fails typechecking

未关闭
#146 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
agent-python community triage
主要语言
Python
星标
74
派生
33
平均合并
9 小时 3 分钟
30 天内合并 PR
1

描述

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.

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 StructlogFormatter.__call__ 开始,检查 event_dict 的类型注解。使用提供的 mypy 示例重现该问题,更新注解使其接受所需的可变 mapping 类型,并确认 mypy 不再报告 list-item 错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
observability-sre
Issue 类型
缺陷
难度
1/5
预计耗时
1 小时以内
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。