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.Formatter does not support key/attribute access on unnumbered fields
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, python-dev, tbeadle, tekknolagi
Priority: normal Keywords: patch

Created on 2016-06-13 15:32 by tbeadle, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
0001-Issue-27307-Support-index-attribute-access-for-unnum.patch tbeadle, 2016-06-13 15:34 review
Pull Requests
URL Status Linked Edit
PR 21767 open python-dev, 2020-08-07 15:32
Messages (5)
msg268452 - (view) Author: Tommy Beadle (tbeadle) * Date: 2016-06-13 15:32
Support for unnumbered fields in string.Formatter.format was added in http://bugs.python.org/issue13598, however, it does not support accessing an index or attribute of an unnumbered field like str.format does.  Instead, it raises an unhelpful "KeyError: ''":

In [1]: import string

In [2]: fmt = string.Formatter()

In [3]: fmt.format('{[0]}', ['a', 'b'])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-a923004fe8e8> in <module>()
----> 1 fmt.format('{[0]}', ['a', 'b'])

/usr/lib64/python2.7/string.pyc in format(*args, **kwargs)
    557                 raise TypeError("format() missing 1 required positional "
    558                                 "argument: 'format_string'")
--> 559         return self.vformat(format_string, args, kwargs)
    560 
    561     def vformat(self, format_string, args, kwargs):

/usr/lib64/python2.7/string.pyc in vformat(self, format_string, args, kwargs)
    561     def vformat(self, format_string, args, kwargs):
    562         used_args = set()
--> 563         result = self._vformat(format_string, args, kwargs, used_args, 2)
    564         self.check_unused_args(used_args, args, kwargs)
    565         return result

/usr/lib64/python2.7/string.pyc in _vformat(self, format_string, args, kwargs, used_args, recursion_depth)
    583                 # given the field_name, find the object it references
    584                 #  and the argument it came from
--> 585                 obj, arg_used = self.get_field(field_name, args, kwargs)
    586                 used_args.add(arg_used)
    587 

/usr/lib64/python2.7/string.pyc in get_field(self, field_name, args, kwargs)
    644         first, rest = field_name._formatter_field_name_split()
    645 
--> 646         obj = self.get_value(first, args, kwargs)
    647 
    648         # loop through the rest of the field_name, doing

/usr/lib64/python2.7/string.pyc in get_value(self, key, args, kwargs)
    603             return args[key]
    604         else:
--> 605             return kwargs[key]
    606 
    607 

KeyError: ''


The attached patch adds this functionality.

aronancher asked in http://bugs.python.org/issue13598#msg218114 if the original patch was going to make it in to python 2.7.  Perhaps that could get a look?
msg364613 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-19 16:53
It would be good if someone could convert this to a pull request and beef up the tests.
msg364623 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2020-03-19 18:35
I'll take a look at the patch and see if this solves my problem. If it does, I'll update my PR with tests.
msg364630 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2020-03-19 18:56
Looks like the patch solves my problem, so I am going to update my PR sometime today.
msg364642 - (view) Author: Maxwell Bernstein (tekknolagi) * Date: 2020-03-19 23:49
Okay, well it doesn't provide the desired behavior of raising the error when switching back and forth between manual and auto numbering, so I am looking into that.
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71494
2020-08-07 15:32:47python-devsetnosy: + python-dev
pull_requests: + pull_request20911
2020-03-19 23:49:45tekknolagisetmessages: + msg364642
2020-03-19 18:56:52tekknolagisetmessages: + msg364630
2020-03-19 18:35:38tekknolagisetmessages: + msg364623
2020-03-19 16:53:18eric.smithsetmessages: + msg364613
2020-03-19 15:22:58eric.smithsetversions: + Python 3.7, Python 3.8, Python 3.9, - Python 3.5, Python 3.6
2020-03-19 15:22:42eric.smithsetnosy: + tekknolagi
2020-03-19 15:22:33eric.smithlinkissue39985 superseder
2016-06-17 09:56:51ned.deilysetnosy: + eric.smith
stage: patch review

versions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4
2016-06-13 15:34:07tbeadlesetfiles: + 0001-Issue-27307-Support-index-attribute-access-for-unnum.patch
keywords: + patch
2016-06-13 15:32:42tbeadlecreate