Unverified Commit 084c38c5 authored by NielsRogge's avatar NielsRogge Committed by GitHub
Browse files

[HF Argparser] Fix parsing of optional boolean arguments (#16946)



* Add fix

* Apply suggestion from code review
Co-authored-by: default avatarNiels Rogge <nielsrogge@Nielss-MacBook-Pro.local>
parent c82e017a
...@@ -103,7 +103,7 @@ class HfArgumentParser(ArgumentParser): ...@@ -103,7 +103,7 @@ class HfArgumentParser(ArgumentParser):
kwargs["default"] = field.default kwargs["default"] = field.default
else: else:
kwargs["required"] = True kwargs["required"] = True
elif field.type is bool or field.type is Optional[bool]: elif field.type is bool or field.type == Optional[bool]:
# Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # Copy the currect kwargs to use to instantiate a `no_*` complement argument below.
# We do not initialize it here because the `no_*` alternative must be instantiated after the real argument # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument
bool_kwargs = copy(kwargs) bool_kwargs = copy(kwargs)
...@@ -140,7 +140,7 @@ class HfArgumentParser(ArgumentParser): ...@@ -140,7 +140,7 @@ class HfArgumentParser(ArgumentParser):
# Order is important for arguments with the same destination! # Order is important for arguments with the same destination!
# We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down
# here and we do not need those changes/additional keys. # here and we do not need those changes/additional keys.
if field.default is True and (field.type is bool or field.type is Optional[bool]): if field.default is True and (field.type is bool or field.type == Optional[bool]):
bool_kwargs["default"] = False bool_kwargs["default"] = False
parser.add_argument(f"--no_{field.name}", action="store_false", dest=field.name, **bool_kwargs) parser.add_argument(f"--no_{field.name}", action="store_false", dest=field.name, **bool_kwargs)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment