Title: argparse: wrong type from get_default when type is set
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Erik Ahlén, paul.j3
Priority: normal Keywords:

Created on 2019-11-07 15:40 by Erik Ahlén, last changed 2019-11-13 05:57 by paul.j3. This issue is now closed.

File name Uploaded Description Edit Erik Ahlén, 2019-11-07 15:40
Messages (4)
msg356194 - (view) Author: Erik Ahlén (Erik Ahlén) Date: 2019-11-07 15:40
The type of the object returned by get_default isn't converted to the specified type supplied to add_argument. I would expect the type to be the same.
msg356222 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-11-08 03:42
get_default just returns the default attribute as it is stored in the Action.   Defaults are stored as given. 

During parsing the Action's default is placed in the args namespace at the start.  At the end, it checks if the value in the namespace is a string that matches the default (i.e. has been over written by user input).  Only then is it passed through the type function.
msg356236 - (view) Author: Erik Ahlén (Erik Ahlén) Date: 2019-11-08 10:20
So, not a bug since you can just do `default = Path('file.txt')`?
msg356247 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-11-08 16:28
Yes you can set the default to be any object, such as an evaluated string.  If it isn't a string it won't be passed through 'type'.

The purpose of the delayed evaluation that I described is to avoid unnecessary evaluations.  The worse case would be a write filetype.  You don't want to create (or over write) a default file if it never gets used.

(I intend to close this issue).
Date User Action Args
2019-11-13 05:57:24paul.j3setstatus: open -> closed
stage: resolved
2019-11-08 16:28:54paul.j3setmessages: + msg356247
2019-11-08 10:20:59Erik Ahlénsetmessages: + msg356236
2019-11-08 03:42:12paul.j3setnosy: + paul.j3
messages: + msg356222
2019-11-07 15:40:47Erik Ahléncreate