pre-commit / pre-commit/pre-commit-hooks

`pretty-format-json` modifies floating point numbers that have too many digits of precision

Abierto
#780 5 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
6.7k
Forks
800
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

pretty-format-json modifies floating point numbers that have too many digits of precision.

Background

I have to work with JSON files that may be generated by non-python programs. These files, for whatever reason, have numbers that have up to 16 digits after the decimal place.

(It's absurd, really. The people taking these measurements are somehow able to measure 0.1μHz on a 10GHz scale?? Yeah, they're saving values like 5.9257052820783001 GHz. Someone needs to teach them about significant figures... but that's beside the point. The point is I have to deal with this data 😒)

Steps to Reproduce
  1. Create the following json file:
    {"foo": 4.4257052820783003}
    
  2. Run pretty-format-json on it.
Expected Output:
{
  "foo": 4.4257052820783003
}
Actual Output:
{
  "foo": 4.4257052820783
}

The diff from expected is:

 {
-  "foo": 4.4257052820783003
+  "foo": 4.4257052820783
 }
Version Info
  • pre-commit: 2.19.0
  • pre-commit-hooks: v4.2.0
  • Python: 3.8.8
  • OS: Debian 11
Discussion

This might be something that has to be fixed within the python builtin json package. A custom JSON encoder/decoder that wraps things using decimal.Decimal might work too.

I've created a test case for this. See my high-precision-numbers branch or the diff.

I'll see if I have time to actually fix this, but I don't expect to ☹️.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

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

Start by reproducing the issue with the pretty-format-json hook and inspect the high-precision-numbers branch and linked diff for its existing test case. Done means the hook preserves the input value 4.4257052820783003 in formatted output without losing decimal digits, with the regression test passing.

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

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
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.