Skip to content

ConfigParser getters not available on SectionProxy #62359

Closed

Description

@jbvsmo
mannequin
BPO 18159
Nosy @ambv, @jbvsmo
Files
  • issue18159-3.diff: Implementation of type converters
  • 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 = 'https://github.com/ambv'
    closed_at = <Date 2014-09-15.09:10:57.031>
    created_at = <Date 2013-06-07.17:43:50.063>
    labels = ['type-feature', 'library']
    title = 'ConfigParser getters not available on SectionProxy'
    updated_at = <Date 2014-09-15.09:10:57.021>
    user = 'https://github.com/jbvsmo'

    bugs.python.org fields:

    activity = <Date 2014-09-15.09:10:57.021>
    actor = 'python-dev'
    assignee = 'lukasz.langa'
    closed = True
    closed_date = <Date 2014-09-15.09:10:57.031>
    closer = 'python-dev'
    components = ['Library (Lib)']
    creation = <Date 2013-06-07.17:43:50.063>
    creator = 'JBernardo'
    dependencies = []
    files = ['30704']
    hgrepos = []
    issue_num = 18159
    keywords = ['patch']
    message_count = 9.0
    messages = ['190767', '190772', '190810', '191891', '193207', '216731', '226900', '226907', '226908']
    nosy_count = 3.0
    nosy_names = ['lukasz.langa', 'python-dev', 'JBernardo']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue18159'
    versions = ['Python 3.5']

    Activity

    1. jbvsmo commented on Jun 7, 2013

      jbvsmomannequin
      MannequinAuthor

      The configparser.RawConfigParser class implements some get methods:
      get, getint, getfloat, getboolean

      but if any of these get overridden on a subclass(with other arguments) or new ones are added (e.g. getlist), there's no way a SectionProxy instance will be able to use them.

          class DemoParser(ConfigParser):
              def getlist(self, section, option):
                  return self.get(section, option).split()
      
          parser = DemoParser()
          parser.read(some_file)
      
          # These 2 lines should be equivalent, like "getint", but the
          # second doesn't work because of the SectionProxy instance
          parser.getlist('some_section', 'foo')
          parser['some_section'].getlist('foo')

      Reading the code, for SectionProxy, it redefines all the get* methods from RawConfigParser, and that looks pretty bad...

      A more elegant approach would be to fetch the function on the parser instance and bound to the section name with lambda or functools.partial... Something like:

          class SectionProxy(...):
              ...
              
              def __getattr__(self, attr):
                  if not attr.startswith('get'):
                      raise AttributeError(attr)
                  fn = getattr(self._parser, attr)
                  return lambda *args, **kw: fn(self._name, *args, **kw)
    2. added
      type-bugAn unexpected behavior, bug, or error
      stdlibStandard Library Python modules in the Lib/ directory
      on Jun 7, 2013
    3. ambv commented on Jun 7, 2013

      @ambv
      Contributor

      This is a reasonable feature request. Overriding __getattr__ doesn't look like the best solution, though. Let me think this over.

    4. self-assigned this
      on Jun 7, 2013
    5. jbvsmo commented on Jun 8, 2013

      jbvsmomannequin
      MannequinAuthor

      Overriding __getattr__ doesn't look like the best solution

      Another idea would be to allow the proxy class to be selectable, but this would require the user to do much more coding for this simple thing...

      I believe a proxy should be dynamic enough to avoid having to duplicate every function definition.

    6. ambv commented on Jun 25, 2013

      @ambv
      Contributor

      There are several reasons why get*() methods are redefined on the section proxy. First of all, explicit is better than implicit. Secondly, the order of arguments is different: parser.get() has the fallback argument as the last (and keyword-only), whereas parser['section'].get() behaves like a mapping. You can do parser['section'].get('key', 'some-fallback-value') and parser['section'].get('no-such-key') returns None instead of raising NoOptionError.

      This makes it difficult to automagically support parser get*() methods on the section proxy. This is why I decided to adapt a different mechanism: register a converter and a getter is automatically available:

        >>> cp = ConfigParser()
        >>> cp.converters['list'] = lambda value: value.strip().split()
        >>> cp.getlist('section', 'l')
        ['a', 'b', 'c']
        >>> cp['section'].getlist('l')
        ['a', 'b', 'c']
        >>> cp.getdict('section', 'd')
        Traceback (most recent call last):
        ...
        AttributeError: 'ConfigParser' object has no attribute 'getdict'
        >>> cp['section'].getdict('d')
        Traceback (most recent call last):
        ...
        AttributeError: 'ConfigParser' object has no attribute 'getdict'

      This ensures that you can easily add new converters in subclasses or single instances and that the parser-level API and section-level API work like they should. This also makes implementing custom getters easier since there's no logic involved besides the conversion. And if you happen to need custom logic anyway, you can register a converter that is a callable class.

    7. added
      type-featureA feature request or enhancement
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Jun 25, 2013
    8. jbvsmo commented on Jul 17, 2013

      jbvsmomannequin
      MannequinAuthor

      Was the patch accepted yet? Looks good to me

    9. jbvsmo commented on Apr 17, 2014

      jbvsmomannequin
      MannequinAuthor

      ping?

    10. ambv commented on Sep 15, 2014

      @ambv
      Contributor

      The reason I didn't commit that patch before was that I wasn't sure whether making this change wouldn't create any unexpected backwards incompatibility.

      In fact, if I committed the patch as is, it would. I solved this by leaving getint, getfloat and getboolean on the parser class and keeping _get in use.

    11. ambv commented on Sep 15, 2014

      @ambv
      Contributor

      The new implementation also automatically covers get*() methods added on subclasses, no need to use converters= in that case.

    12. python-dev commented on Sep 15, 2014

      python-devmannequin
      Mannequin

      New changeset 2c46a4ded259 by Łukasz Langa in branch 'default':
      Closes bpo-18159: ConfigParser getters not available on SectionProxy
      https://hg.python.org/cpython/rev/2c46a4ded259

      New changeset 5eb95d41ee43 by Łukasz Langa in branch 'default':
      Closes bpo-18159: ConfigParser getters not available on SectionProxy
      https://hg.python.org/cpython/rev/5eb95d41ee43

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions