This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.format() gives poor diagnostic on placeholder mismatch
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Mariatta, dmzz, eric.smith, ezio.melotti, franciscouzo, nanjekyejoannah, roysmith, terry.reedy, tshepang
Priority: normal Keywords: easy, patch

Created on 2014-06-29 14:37 by roysmith, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
format.patch franciscouzo, 2016-09-09 07:53 review
Pull Requests
URL Status Linked Edit
PR 12675 merged franciscouzo, 2019-04-03 16:38
Messages (12)
msg221846 - (view) Author: Roy Smith (roysmith) Date: 2014-06-29 14:37
https://mail.python.org/pipermail/python-list/2014-June/674188.html
msg221847 - (view) Author: Roy Smith (roysmith) Date: 2014-06-29 14:41
(ugh, hit return too soon)

>>> '{1}'.format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

This is a confusing error message.  The user hasn't written any tuples, so a message about a tuple index out of range will just leave them scratching their head.  We should either return a more specific subclass of IndexError, or at least a more descriptive text describing what went wrong.

See https://mail.python.org/pipermail/python-list/2014-June/674188.html for background.
msg221855 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-06-29 16:02
IndexError should be caught and replaced with something like
ValueError('format string requests argument not passed') or
TypeError('arguments do not match format string') or more specific
TypeError('format string requests at least %d positional arguments, only %d passed')
In Roy's example (a good testcase), the numbers would be 1 and 0.
msg222514 - (view) Author: Tshepang Lekhonkhobe (tshepang) * Date: 2014-07-07 20:50
@terry would a change like this be accepted in 2.7 and 3.4?
msg222524 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-07-07 21:46
I don't think we can change the exception type in a bugfix release, if ever. I don't see much point in changing the exception type: it's just churn.

I'm +1 for a better error message. I'm -0 on changing the error message in a bugfix release.
msg222545 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-07-08 05:15
After reading Eric's comments and checking the signature of .format and rereading the Format String Syntax section, I have changed my mind. The signature of .format is "S.format(*args, **kwargs) -> str" and args is a tuple. So the user asks for creation of a tuple by calling .format. The only unusual part is the the user also provides indexes into the args tuple, but the doc is clear enough that ints passed as specification field names select positional arguments. I  think the current message is correct enough to leave alone in current releases. An improved message might be

IndexError("Replacement index %d out of range for positional args tuple")

This uses 'replacement index' as a contraction of 'integer passed as replacement field name'.
msg336669 - (view) Author: Carlos Damázio (dmzz) Date: 2019-02-26 13:40
I've noticed this issue is quite not active, but I'm up on participating in it if anyone is doing it already. Since then, this issue still persists:

>>> '{1}'.format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

Reading further, I agree that improving the message is suitable. We should leave the Index Exception while changing the text to "Replacement index %d out of range for positional args tuple". Even though I find it quite extent for an exception message, I guess it couldn't be any other way, unless someone gives a better suggestion.
msg336672 - (view) Author: Carlos Damázio (dmzz) Date: 2019-02-26 14:03
Ops, someone already patched it! Sorry guys.
msg336843 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-02-28 15:51
@dmzz, You can convert the patch into a PR on github or Open a PR with your own implementation.

If working with this same patch, the next step would be to test the patch if it is still in good shape now before opening a PR.
msg336845 - (view) Author: Francisco Couzo (franciscouzo) * Date: 2019-02-28 16:05
I'm preparing the PR myself
msg339399 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-04-03 15:58
@franciscouzo any status on this? Do you need any help on this?
msg339400 - (view) Author: Francisco Couzo (franciscouzo) * Date: 2019-04-03 16:39
I just made the pull request, I totally forgot I mentioned I was going to work on this, sorry!
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66078
2019-06-01 17:14:57rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-04-03 16:39:43franciscouzosetmessages: + msg339400
2019-04-03 16:38:14franciscouzosetstage: needs patch -> patch review
pull_requests: + pull_request12602
2019-04-03 15:58:13nanjekyejoannahsetmessages: + msg339399
2019-03-04 21:00:54ceronmansetnosy: - ceronman
2019-02-28 16:05:08franciscouzosetmessages: + msg336845
2019-02-28 15:51:02nanjekyejoannahsetnosy: + nanjekyejoannah
messages: + msg336843
2019-02-26 14:03:41dmzzsetmessages: + msg336672
2019-02-26 13:40:24dmzzsetnosy: + dmzz
messages: + msg336669
2019-02-24 21:37:32ceronmansetnosy: + ceronman
2016-09-29 21:43:45Mariattasetnosy: + Mariatta
2016-09-09 12:00:54eric.smithsetassignee: eric.smith
versions: + Python 3.6, - Python 3.5
2016-09-09 07:55:17franciscouzosetnosy: + franciscouzo
2016-09-09 07:53:56franciscouzosetfiles: + format.patch
keywords: + patch
2014-07-08 05:15:02terry.reedysettype: behavior -> enhancement
messages: + msg222545
versions: - Python 2.7, Python 3.4
2014-07-07 21:46:30eric.smithsetmessages: + msg222524
2014-07-07 20:50:24tshepangsetmessages: + msg222514
2014-07-07 20:44:00tshepangsetnosy: + tshepang
2014-07-05 12:50:55ezio.melottisetkeywords: + easy
nosy: + ezio.melotti
components: + Library (Lib)
2014-06-29 16:02:43terry.reedysetversions: + Python 3.4, Python 3.5
nosy: + terry.reedy, eric.smith

messages: + msg221855

stage: needs patch
2014-06-29 14:41:02roysmithsettype: behavior
messages: + msg221847
versions: + Python 2.7
2014-06-29 14:37:28roysmithcreate