-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with casting argument to bool when target variable has type Union[bool, str] #379
Comments
That is weird. I tried the code above with python 3.10 in ubuntu 22.04 and got: $ ./issue_379.py --plop true
Namespace(plop=True) |
Ah wait, it depends on the order in the typing. If |
This is the expected behavior. For unions it goes from left to right checking if the value is valid for the subtype. It stops when the value is valid. So if str is first, it will stay as str. |
I see. I wonder if that could be amended as this can lead to silent failures. It feels like |
This behavior will not be changed. Explaining that all subtypes are checked left to right is easy to explain and understand. Some arbitrary priority that overrides the general rule only for some types would lead to more confusion. My main question would be, why is the type |
This is the exact package and parameter I have trouble with! Good to see the typing will be fixed. |
馃悰 Bug report
If an argument is typed as
bool | str
, when trying to set that argument toTrue
from command line it will instead be set to stringtrue
.To reproduce
Create a file test.py
From command line,
python test.py --plop true
Expected behavior
Expected behaviour would be to have
cfg = {"plop": True}
. Instead we getcfg = {"plop": "true"}
.It works as intended if parameter is set through a config file when using the CLI object.
But it always fails from command line.
Note, it works if the typing is
bool|str
Environment
pip install jsonargparse[all]
): pip install jsonargparseThe text was updated successfully, but these errors were encountered: