python / python/cpython

Undocumented handling of thousands separator for floats and differences wrt Decimal

Aberta
#130,664 2 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

docs extension-modules stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

In docs we have:

The ',' option signals the use of a comma for a thousands separator for floating-point presentation types and for integer presentation type 'd'. For other presentation types, this option is an error. For a locale aware separator, use the 'n' integer presentation type instead.

The '_' option signals the use of an underscore for a thousands separator for floating-point presentation types and for integer presentation type 'd'. For integer presentation types 'b', 'o', 'x', and 'X', underscores will be inserted every 4 digits. For other presentation types, specifying this option is an error.

But this doesn't specify how separators are inserted for customized alignment, consider:

>>> format(0.0, '>020_f')
'0000000000000.000000'
>>> format(0.0, '<020_f')
'0.000000000000000000'
>>> format(0.0, '=020_f')
'0_000_000_000.000000'
>>> format(0.0, '^020_f')
'0000000.000000000000'

Probably it's obvious, that separators might be inserted only in case of 0-padding and = alignment, though maybe this should be mentioned explicitly.

Note also that neither from above supported by Decimal/Fraction, e.g.:

>>> format(Decimal(0.0), '=020_f')
Traceback (most recent call last):
  File "<python-input-13>", line 1, in <module>
    format(Decimal(0.0), '=020_f')
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid format string
>>> format(Fraction(0.0), '=020_f')
'            0.000000'
>>> format(float(Fraction(0.0)), '=020_f')
'0_000_000_000.000000'

BTW, the rationale for Fraction's behavior (see https://github.com/python/cpython/pull/100161) is "no special-casing of the particular '0=' fill-character/alignment combination", which is rather weak, IMO.

Either this should be fixed (preferred) or documented.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-131030
  • gh-131067
  • gh-132155
  • gh-136241
  • gh-136242
  • gh-136649

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece lendo a documentação da format specification mini-language e reproduza os exemplos de float, Decimal e Fraction na issue. Revise os PRs vinculados para entender o trabalho que já está em andamento. Está concluído quando o comportamento do separador e do alinhamento for resolvido de forma consistente ou quando as regras observadas forem documentadas.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
documentation
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.