python / python/cpython

Improve visibility of key type coercion limitation in json module docs

オープン
#137,533 コメント 0 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs stdlib
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Documentation

Hi team,

I’d like to suggest enhancing the JSON module documentation by placing the warning about non‑string dictionary keys being coerced to strings more prominently, ideally at the very beginning of the module overview or in a shared introductory note.

Currently, that note appears under json.dumps() and reads:

“Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings. As a result of this, if a dictionary is converted into JSON and then back into a dictionary, the dictionary may not equal the original one. That is, loads(dumps(x)) != x if x has non‑string keys.”
docs.python.org/3/library/json.html#json.dumps

Even though it's technically accurate, it's tucked away in a method-specific section. Given how fundamental this behavior is—and how easy it is to overlook—it should be surfaced at the module level, so users understand that it applies to both dump() and dumps().


Why it matters

The JSON standard (RFC 7159) explicitly states:

“An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.”
— RFC 7159 §1
https://datatracker.ietf.org/doc/html/rfc7159#section-1

While this is unambiguous, not all Python users are aware of the JSON RFC, and it's not realistic to expect them to be. Many developers rely solely on Python's documentation as their interface to the JSON format. That's why it's crucial that the behavior of coercing non-string keys is clearly stated up front—before diving into function-specific details.

This helps avoid silent bugs where json.loads(json.dumps(x)) != x, and removes the expectation that dicts with int or bool keys will round-trip without transformation.


@facundobatista — would you mind taking a look?

Linked PRs
  • gh-137545

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Python JSON モジュールのドキュメントと、json.dumps() セクションにある既存の警告から始めます。文字列ではない辞書キーの強制変換に関する警告を、モジュール概要または共通の導入部に示し、それが dump() と dumps() の両方に適用されることを明確にします。この動作が関数ごとの詳細より前に確認できれば、ドキュメントは完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。