zenlotus / argparse

Automatically exported from code.google.com/p/argparse
Other
0 stars 0 forks source link

Incorrect type for default= in add_argument() is silently ignored, should raise exception #60

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Make a mistake and declaring the default value to be of a different type
than the 'type' declared in the same parameter list

What is the expected output? What do you see instead?

I'd hope that I'd get an assertion error that the default value is not of
the correct type, the error currently passes silently.

What version of the product are you using? On what operating system?

1.1a1 from svn, At revision 80. On OS X 10.6.

Please provide any additional information below.
    parser_csv.add_argument(
        '-c', '--contents', type=str,
        choices=['kwd_desc', 'stuff','otherstuff'],
        default=['kwd_desc'],
        help='what data to put in the report')

The default is a list, when the arg type is supposed to be string.  A
simple mistake, and one that I'd like argparse to catch.

Original issue reported on code.google.com by sstein...@gmail.com on 14 Feb 2010 at 2:49

GoogleCodeExporter commented 9 years ago
I'm not sure how to fix this in general -- "type" doesn't have to actually be a 
type.
For example, the following is okay:

  >>> parser = argparse.ArgumentParser()
  >>> action = parser.add_argument('--foo', type=lambda x: int(x) + 1, default=2)
  >>> parser.parse_args(['--foo', '3'])
  Namespace(foo=4)
  >>> parser.parse_args([])
  Namespace(foo=2)

But I can't do an isinstance check on the action's type::

  >>> isinstance(2, action.type)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

I'm open to patches though if you see a way to get your error caught without
provoking the above error.

Original comment by steven.b...@gmail.com on 1 Mar 2010 at 7:22

GoogleCodeExporter commented 9 years ago
Closing as WontFix. If you have a specific alternative behavior you'd like 
implemented, please file a new issue at bugs.python.org.

Original comment by steven.b...@gmail.com on 23 Jul 2010 at 12:51