Title: Refer to actual format string when creating “zero padding” error message
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bignose, eric.smith, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-08-16 04:35 by bignose, last changed 2018-12-20 22:10 by serhiy.storchaka.

Pull Requests
URL Status Linked Edit
PR 11270 open serhiy.storchaka, 2018-12-20 22:03
Messages (2)
msg272827 - (view) Author: Ben Finney (bignose) Date: 2016-08-16 04:35
When using a format specifier with leading zero, the format spec mini-language (as documented at>) says:

> '=' […] becomes the default when ‘0’ immediately precedes the field width.

When the ‘=’ option is only implied, the error message “ValueError: '=' alignment not allowed in string format specifier” becomes surprising and incomprehensible to someone who does not know that implied behaviour.

In issue 15560, Terry Reedy says:

> If the spec string is still available, it could be searched and the message adjusted if '=' is not present.  That proposal should be a new issue if someone wants to push it.

This issue raises that proposal.

The error message should be changed so that:

* It makes sense whether or not the ‘=’ option is explicit in the format specifier.


* Different messages are produced when the ‘=’ option is explicit versus when it is implicit.

I think the former option is better, but either will satisfy this request.
msg332271 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-20 22:10
PR 11270 fixes this issue by making such format valid. Preceding the width field by '0' no longer affects the default alignment for strings, i.e. no longer sets the alignment to invalid '=' if it is not specified explicitly.

>>> format('abc', '<8')
'abc     '
>>> format('abc', '8')
'abc     '
>>> format('abc', '<08')
>>> format('abc', '08')

This does not contradict the documentation, which specifies this case for numeric types, but not for strings.
Date User Action Args
2018-12-20 22:10:31serhiy.storchakasetcomponents: + Interpreter Core
versions: + Python 3.8, - Python 2.7, Python 3.5, Python 3.6
2018-12-20 22:10:12serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg332271
2018-12-20 22:03:36serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request10506
2016-08-16 04:39:57ned.deilysetnosy: + eric.smith

versions: + Python 3.6, - Python 3.2, Python 3.3, Python 3.4
2016-08-16 04:35:18bignosecreate