[New-bugs-announce] [issue16516] argparse types (and actions) must be hashable

Joel Nothman report at bugs.python.org
Wed Nov 21 04:28:27 CET 2012


New submission from Joel Nothman:

The argparse documentation states that "type= can take any callable that takes a single string argument and returns the converted value". The following is an exception:

    >>> import argparse
    >>> ap = argparse.ArgumentParser()
    >>> ap.add_argument('foo', type={}.get)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3/argparse.py", line 1294, in add_argument
        type_func = self._registry_get('type', action.type, action.type)
      File "/usr/lib/python3/argparse.py", line 1236, in _registry_get
        return self._registries[registry_name].get(value, default)
    TypeError: unhashable type: 'dict'

Sadly, a method bound to an unhashable type is unhashable! (Of course, is trivial to use partial or lambda to make any function hashable.)

The offending line in _registry_get is intended to look up named types (or actions), so it perhaps only relevant for string type= and action= values, which could be handled explicitly instead of using dict.get(x, x) to handle both cases. Alternatively, the TypeError could be caught and default returned.

----------
components: Library (Lib)
messages: 176040
nosy: bethard, jnothman
priority: normal
severity: normal
status: open
title: argparse types (and actions) must be hashable
type: behavior
versions: Python 2.7, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16516>
_______________________________________


More information about the New-bugs-announce mailing list