Skip to content

string.Formatter does not support key/attribute access on unnumbered fields #71494

Closed

Description

@tbeadle
mannequin
BPO 27307
Nosy @ericvsmith, @tekknolagi
PRs
  • gh-71494: string.Formatter unnumbered key/attributes #21767
  • Files
  • 0001-Issue-27307-Support-index-attribute-access-for-unnum.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-06-13.15:32:42.624>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = 'string.Formatter does not support key/attribute access on unnumbered fields'
    updated_at = <Date 2020-08-07.15:32:47.479>
    user = 'https://bugs.python.org/tbeadle'

    bugs.python.org fields:

    activity = <Date 2020-08-07.15:32:47.479>
    actor = 'python-dev'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-06-13.15:32:42.624>
    creator = 'tbeadle'
    dependencies = []
    files = ['43376']
    hgrepos = []
    issue_num = 27307
    keywords = ['patch']
    message_count = 5.0
    messages = ['268452', '364613', '364623', '364630', '364642']
    nosy_count = 4.0
    nosy_names = ['eric.smith', 'python-dev', 'tbeadle', 'tekknolagi']
    pr_nums = ['21767']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue27307'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    Activity

    1. tbeadle commented on Jun 13, 2016

      tbeadlemannequin
      MannequinAuthor

      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'])
      \---------------------------------------------------------------------------
      ```pytb
      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?

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jun 13, 2016
    3. ericvsmith commented on Mar 19, 2020

      @ericvsmith
      Member

      It would be good if someone could convert this to a pull request and beef up the tests.

    4. tekknolagi commented on Mar 19, 2020

      tekknolagimannequin
      Mannequin

      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.

    5. tekknolagi commented on Mar 19, 2020

      tekknolagimannequin
      Mannequin

      Looks like the patch solves my problem, so I am going to update my PR sometime today.

    6. tekknolagi commented on Mar 19, 2020

      tekknolagimannequin
      Mannequin

      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.

    7. transferred this issue fromon Apr 10, 2022
    8. added a commit that references this issue on Jan 31, 2025
    9. encukou commented on Jan 31, 2025

      @encukou
      Member

      Fixed by: #21767

    10. added a commit that references this issue on Feb 7, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions