python / python/cpython

Incomplete documentation of zero preceding width in format specification

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

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

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

説明

The current description of zero preceding width in format is the following:

When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types, excluding complex. This is equivalent to a fill character of '0' with an alignment type of '='.

This is not complete. Consider the next code snippet:

formats = (
    ' <+#20.0f',
    '<+#20.0f',
    '+#20.0f',

    ' <+#020.0f',
    '<+#020.0f',
    '+#020.0f',
    )

for fmt in formats:
    print(f'Format {fmt!r:>12}: {format(1, fmt)!r}')

It has the following output:

Format  ' <+#20.0f': '+1.                 '
Format   '<+#20.0f': '+1.                 '
Format    '+#20.0f': '                 +1.'
Format ' <+#020.0f': '+1.                 '
Format  '<+#020.0f': '+1.00000000000000000'
Format   '+#020.0f': '+000000000000000001.'

The first three lines do not precede width of 20 with zero, remaining three do. Lines 4 and 5 show that one can precede width with zero even if alignment is specified. Specifically:

  1. If both fill char and alignment are present, then zero is ignored.
  2. If only alignment is present, then zero sets omitted fill char to 0 (omitting fill char by default uses space as demonstrated by line 2).

Those two cases are not covered; line 6 shows the case covered by docs.


Present docs excludes complex numbers, but it is a bit tricky. One can precede width with zero only if both fill char and alignment are present (i.e., when it is ignored), otherwise an exception is raised. This case can be completely omitted as the same exception also happens when fill char itself is zero (f'{1+1j:0<+#20.0f}'). Instead, the fact that complex numbers cannot be zero-padded should be added to the description of fill char.

Linked PRs
  • gh-132149

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

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

はじめの一歩

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

調査の方向性

issue で説明されている format-specification ドキュメントから始め、ゼロ幅に関する記述を、提供されている Python の例と比較してください。明示的な fill と alignment の組み合わせについての説明を更新し、複素数に関する制限を明確にしたうえで、ドキュメントに記載されたケースが示されている出力と一致することを確認してください。

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

評価

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

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

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