Message 253193 - Python tracker

Message253193

Author paul.j3
Recipients berker.peksag, dfortunov, paul.j3
Date 2015-10-19.21:17:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1445289435.45.0.735396812338.issue25436@psf.upfronthosting.co.za>
In-reply-to
Content
The short `repr` is produced in Python2.7, but not 3.5.

Your test case:

    action = argparse._StoreAction(['--file], dest='file_path')
    error = argparse.ArgumentError(action, 'File not found.')
    print(str(error))
    print(repr(error))

With Python3.5 this displays:

    argument --file: File not found.

    ArgumentError(_StoreAction(option_strings=['--file'],
       dest='file_path', nargs=None, const=None, default=None,
       type=None, choices=None, help=None, metavar=None), 
       'File not found.')

In Python2.7 the `repr()` produces:

    ArgumentError()

Your patch changes the `repr` to:

    ArgumentError('File not found.', '--file')

I think you could achieve the same effect by defining a `__repr__` method for `ArgumentError`.  

By the way, to produce such an Action, you would normally use:

    parser.add_argument('--file')

'--file=/foo/bar' is not a good argument option flag.  The code may accept it, but it will confuse your users.


--------------

Normally an `ArgumentError` is raised during parsing, and is caught at the end of `parse_known_args` with:

        except ArgumentError:
            err = _sys.exc_info()[1]
            self.error(str(err))

So its `str()` is passed on the the `parser.error` method (and on to `parser.exit`).  Normally a user, or developer's code will not see this error class.

The purpose of `ArgumentError` is to clearly identify a class of error, and to add the `Action` identity to the error message.  It's part of the API so custom Action classes can use it to pass errors through the normal parsing error system.

Can you make a better case for needing an improved `repr`?  How would it be logged or otherwise be made visible to users and/or developers?
History
Date User Action Args
2015-10-19 21:17:15paul.j3setrecipients: + paul.j3, berker.peksag, dfortunov
2015-10-19 21:17:15paul.j3setmessageid: <1445289435.45.0.735396812338.issue25436@psf.upfronthosting.co.za>
2015-10-19 21:17:15paul.j3linkissue25436 messages
2015-10-19 21:17:14paul.j3create