Issue 17247: int and float should detect inconsistent format strings (original) (raw)

Created on 2013-02-19 22:24 by christian.heimes, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
issue17247.diff skrah,2013-05-29 22:02 review
Messages (7)
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 "", line 1, in 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 "", line 1, in 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.
msg240391 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-09 21:08
This would need a deprecation preriod if we want to do it.
History
Date User Action Args
2022-04-11 14:57:42 admin set github: 61449
2015-04-09 21:08:28 r.david.murray set nosy: + r.david.murraymessages: + versions: + Python 3.5, - Python 3.4
2014-10-14 16:40:53 skrah set nosy: - skrah
2013-05-29 22:02:21 skrah set files: + issue17247.diffversions: - Python 3.3messages: + keywords: + patchstage: needs patch -> patch review
2013-02-20 01:31:08 eric.smith set nosy: + eric.smith
2013-02-19 23:26:09 skrah set assignee: skrah -> messages: + components: + Interpreter Coretitle: Decimal doesn't support aligned fill -> int and float should detect inconsistent format strings
2013-02-19 22:50:00 skrah set messages: +
2013-02-19 22:36:36 christian.heimes set messages: +
2013-02-19 22:34:53 skrah set messages: +
2013-02-19 22:26:59 ezio.melotti set nosy: + mark.dickinson, ezio.melottistage: needs patch
2013-02-19 22:24:53 christian.heimes create