python / python/typeshed

json.dump/dumps shouldn't allow `Any` when `default` is None

未关闭
#13,781 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stubs: false negative
主要语言
Python
星标
5.1k
派生
2.1k
平均合并
1 天 19 小时
30 天内合并 PR
82

描述

When default is None, we know what types are serialisable.

Thus,

import json
import types
import typing

def x(data: typing.Mapping[str, str]) -> str:
    return json.dumps(data)

def y():
    return json.dumps(types.MappingProxyType({}))

Shouldn't type-check.

Running this fails with:

File /opt/homebrew/Cellar/python@3.13/3.13.2/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py:180, in JSONEncoder.default(self, o)
    161 def default(self, o):
    162     """Implement this method in a subclass such that it returns
    163     a serializable object for ``o``, or calls the base implementation
    164     (to raise a ``TypeError``).
   (...)    178 
    179     """
--> 180     raise TypeError(f'Object of type {o.__class__.__name__} '
    181                     f'is not JSON serializable')

TypeError: Object of type mappingproxy is not JSON serializable

Type-checking JSONEncoder itself would be much harder, but the more-commonly-used functions should be doable via overloads.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

定位 json.dump 和 json.dumps 的存根,并检查它们的重载如何处理默认参数。根据所得类型检查 issue 中的示例;完成的标准是:不带默认参数的调用会拒绝不受支持的输入,而带默认参数的调用仍保持适当的类型。

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

评估

技术栈
python
领域
devtools
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

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