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: String formatting: grammar wrongly limits [index] to integer
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eddy, eric.smith, georg.brandl
Priority: normal Keywords: patch

Created on 2010-02-14 11:28 by eddy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue7928.diff eric.smith, 2010-02-22 13:40
Messages (4)
msg99340 - (view) Author: Edward Welbourne (eddy) Date: 2010-02-14 11:28
http://docs.python.org/library/string.html#formatstrings
field_name        ::=  (identifier | integer) ("." attribute_name | "[" element_index "]")*
element_index     ::= integer

Subsequent text indicates __getitem__() is used but does not overtly say that a string can be used; but
http://docs.python.org/whatsnew/2.6.html#pep-3101-advanced-string-formatting
gives the example
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
and clearly '.mp4' is passed to __getitem__(); a string, not an integer.

Clearly one of these is wrong !
Given that the "what's new" doc goes into some detail about how the content of [...] gets parsed, I'm guessing it's right and the grammar is wrong.
msg99732 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-22 13:31
I'm not exactly sure what wording to use here.

      element_index: `integer` | `identifier`

is not exactly correct, because it can be a non-identifier (as the example that eddy quotes points out. It's really "any sequence of characters except ']'".

Any ideas on the best way to express that? Maybe taking a clue from string literals, this would be:
      element_index: `integer` | index_string
      index_string: <any source character except "]"> +
msg99734 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-22 13:40
Proposed patch attached. The rest of the documentation in the following 2 paragraphs looks correct. It refers to __getitem__, which is how either strings or integers is looked up.
msg100089 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-25 14:59
Checked in.

trunk: r78444
release26-maint: r78445
py3k: r78446
release31-maint: r78447
History
Date User Action Args
2022-04-11 14:56:57adminsetgithub: 52176
2010-02-25 14:59:59eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg100089

stage: patch review -> resolved
2010-02-24 17:51:38eric.smithsetpriority: normal
2010-02-22 13:40:26eric.smithsetfiles: + issue7928.diff
keywords: + patch
messages: + msg99734

stage: patch review
2010-02-22 13:32:26eric.smithsetversions: + Python 3.1, Python 2.7, Python 3.2
2010-02-22 13:31:39eric.smithsetmessages: + msg99732
2010-02-14 13:16:41georg.brandlsetassignee: georg.brandl -> eric.smith

nosy: + eric.smith
2010-02-14 11:28:06eddycreate