classification
Title: argparse parse_args args parameter bug or intended
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Gharg, paul.j3, remi.lapeyre, rhettinger
Priority: normal Keywords:

Created on 2020-04-16 16:47 by Gharg, last changed 2020-04-17 08:31 by Gharg. This issue is now closed.

Messages (4)
msg366608 - (view) Author: Gharg (Gharg) Date: 2020-04-16 16:47
I have a problem regarding args parameter of ArgumentParser.parse_args.

For example:
-------------------------------------
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--boolean", type=bool)
parsed_args = parser.parse_args(["--boolean=''"])
--------------------------------------

results in parsed_args.boolean evaluate to True.
While i understand why this is happening (inner call of bool("''") evaluates to True), i don't know if that is an expected behavior.
If we look from console argument pass perspective with the example altered:
test.py
-------------------------------------
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--boolean", type=bool)
parsed_args = parser.parse_args()
--------------------------------------

If i now call:

python test.py --boolean=""

parsed_args.boolean will evaluate to False.
msg366626 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-04-16 19:39
Hi Gharg, this is expected, both because your program would not actually receive `--boolean=''` but `--boolean=`:

➜  ~ cat test.py
import sys

print(sys.argv)
➜  ~ python test.py --boolean=''
['test.py', '--boolean=']

and the way the type argument works. 

You can do what you are looking for by using:

➜  ~ cat test.py
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--boolean', action='store_const', const=True, default=False)

print(parser.parse_args())
➜  ~ python test.py --boolean
Namespace(boolean=True)
➜  ~ python test.py
Namespace(boolean=False)
msg366627 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-04-16 20:23
'type=bool' doesn't get any special treatment.  'bool' is a builtin Python function.

test with 

    def mybool(astr):
        print("mybool:", astr, len(astr), bool(astr))
        return bool(astr)

The trick to getting a False value is to pass a genuinely empty string.

As can be seen in several previous bug/issues, 'bool' is allowed as a type function, but since the only string that returns False is the empty one, it isn't very useful.  And we've resisted attempts give it some special treatment.

Users can write their own 'type' function that accepts language specific 'true/false' words.  Otherwise we encourage the use of 'store_true', 'store_false' action values.
msg366646 - (view) Author: Gharg (Gharg) Date: 2020-04-17 08:31
Hi Rémi and Paul,

thanks for your quick response.
It is as i expected and i will use 'store_true'/'store_false' to resolve my problem.

I see this issue as resolved and close it.
History
Date User Action Args
2020-04-17 08:31:15Ghargsetstatus: open -> closed
resolution: not a bug
messages: + msg366646

stage: resolved
2020-04-16 20:23:27paul.j3setmessages: + msg366627
2020-04-16 19:39:54remi.lapeyresetnosy: + remi.lapeyre
messages: + msg366626
2020-04-16 19:10:59xtreaksetnosy: + rhettinger, paul.j3
2020-04-16 16:47:14Ghargcreate