python / python/cpython

Improve visibility of key type coercion limitation in json module docs

Abierto
#137,533 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

docs stdlib
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza por la documentación del módulo JSON de Python y la advertencia existente en la sección de json.dumps(). Haz visible la advertencia sobre la coerción de claves de diccionario que no son cadenas en la descripción general del módulo o en la introducción compartida, dejando claro que se aplica tanto a dump() como a dumps(); la documentación estará terminada cuando este comportamiento sea visible antes de los detalles específicos de cada función.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
documentation
Tipo de issue
Documentación
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.