classification
Title: int and float should detect inconsistent format strings
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, eric.smith, ezio.melotti, mark.dickinson, skrah
Priority: normal Keywords: patch

Created on 2013-02-19 22:24 by christian.heimes, last changed 2013-05-29 22:02 by skrah.

Files
File name Uploaded Description Edit
issue17247.diff skrah, 2013-05-29 22:02 review
Messages (6)
msg182447 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-02-19 22:24
>>> "{:<06}".format(1.2)
'1.2000'
>>> "{:<06}".format(decimal.Decimal(1.2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid format string
msg182449 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-02-19 22:34
3.2 has a better error message:

>>> "{:<06}".format(Decimal("1.2"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/decimal.py", line 3632, in __format__
    spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
  File "/usr/lib/python3.2/decimal.py", line 5600, in _parse_format_specifier
    "format specifier: " + format_spec)
ValueError: Alignment conflicts with '0' in format specifier: <06


That's because '0' already has a special meaning:

"Preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='."
msg182450 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-02-19 22:36
The output is from Python 3.3. Why has Python 3.3 a less informative error message than 3.2?

I also wonder why it works with floats if it has a special meaning? Either float or Decimal is broken.
msg182451 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-02-19 22:50
Christian Heimes <report@bugs.python.org> wrote:
> The output is from Python 3.3. Why has Python 3.3 a less informative error message than 3.2?

Because the error is discovered in libmpdec and it would require a significant
amount of work to provide fine-grained error messages.

> I also wonder why it works with floats if it has a special meaning?
> Either float or Decimal is broken.

Yes, IMO float should detect the ambiguity. You see that the zero implies left
padding:

>>> "{:06}".format(1.2)
'0001.2'

And in case of a conflict right padding wins:

>>> "{:<06}".format(1.2)
'1.2000'

The unambiguous way to get right padding is:

>>> "{:0<6}".format(Decimal("1.2"))
'1.2000'

>>> "{:X<6}".format(Decimal("1.2"))
'1.2XXX'
msg182452 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-02-19 23:26
With int and float it's also possible to specify both conflicting
alignments and fill characters:


 >>> "{:x<06}".format(1.2)
'1.2xxx'


So I really think that the builtins should be changed to detect
the conflict.
msg190331 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2013-05-29 22:02
With this patch float and int should behave like Decimal. It may
break existing code that (accidentally) uses both legacy zero padding
and explicit alignment.
History
Date User Action Args
2013-05-29 22:02:21skrahsetfiles: + issue17247.diff
versions: - Python 3.3
messages: + msg190331

keywords: + patch
stage: needs patch -> patch review
2013-02-20 01:31:08eric.smithsetnosy: + eric.smith
2013-02-19 23:26:09skrahsetassignee: skrah ->
messages: + msg182452
components: + Interpreter Core
title: Decimal doesn't support aligned fill -> int and float should detect inconsistent format strings
2013-02-19 22:50:00skrahsetmessages: + msg182451
2013-02-19 22:36:36christian.heimessetmessages: + msg182450
2013-02-19 22:34:53skrahsetmessages: + msg182449
2013-02-19 22:26:59ezio.melottisetnosy: + mark.dickinson, ezio.melotti

stage: needs patch
2013-02-19 22:24:53christian.heimescreate