python / python/cpython

Undocumented handling of thousands separator for floats and differences wrt Decimal

Đang mở
#130,664 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

docs extension-modules stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc đọc tài liệu về format specification mini-language và tái hiện các ví dụ về float, Decimal và Fraction trong issue. Xem lại các PR được liên kết để hiểu công việc đang được tiến hành. Được xem là hoàn tất khi hành vi của dấu phân cách và căn chỉnh được giải quyết một cách nhất quán hoặc các quy tắc quan sát được được ghi lại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
documentation
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.