HfArgumentParser defaults booleans to on
See original GitHub issueEnvironment info
transformers
version: 4.9.0.dev0 (initially discovered on 4.8.1)- Platform: macOS-11.4-x86_64-i386-64bit
- Python version: 3.9.5
- PyTorch version (GPU?): not installed (NA)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
Who can help
Information
HfArgumentParser when used on a dataclass with a bool field with no default turns the bool on unless it’s supplied with --<field_name> False
or similar “false-y” value. I would expect that as the field has no default it should be false unless --<field_name>
or --<field_name> True
is supplied. This is a behaviour change from v3.0.0
where the booleans are parsed correctly as we’re looking at upgrading and this issue hit us.
To reproduce
Steps to reproduce the behavior:
- Define a dataclass with a boolean field
- Supply a list of arguments which does not include that field name
- The field is turned on.
Appending this snippet to the bottom of test_basic
at line 110 in test_hf_argparser.py
fails the test.
args = ["--foo", "1", "--baz", "quux", "--bar", "0.5"]
example, = parser.parse_args_into_dataclasses(args, look_for_args_file=False)
self.assertFalse(example.flag)
Extending args
with ["--flag","False"]
recovers the expected behaviour.
Expected behavior
The boolean should be set to false if the argument is not passed in.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
allowing the user to set booleans with HfArgumentParser #9224
Hi this is more general issue, if the user defines some booleans as arguments like --flag false/true once calling the finetune_trainer.py ...
Read more >Source code for transformers.hf_argparser - Hugging Face
[docs]class HfArgumentParser(ArgumentParser): """ This subclass of `argparse. ... MISSING: kwargs["default"] = field.default elif field.type is bool or ...
Read more >Parsing boolean values with argparse - python - Stack Overflow
This solution will gurantee you always get a bool type with value True or False . (This solution has a constraint: your option...
Read more >python/huggingface/transformers/tests/utils/test_hf_argparser.py
A boolean no_* argument always has to come after its "default: True" regular counter-part ... parser = HfArgumentParser(EnumExample) expected = argparse.
Read more >Python Booleans: Optimize Your Code With Truth Values
In this tutorial, you'll learn about the built-in Python Boolean data type, ... in combination with short-circuit evaluation in order to have a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, it does look like a bug. Fix is quite simple, I can make a PR.
Ok, I’ll check it tomorrow against our internal use case to make sure it fixes that too.