question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HfArgumentParser defaults booleans to on

See original GitHub issue

Environment 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

@sgugger

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:

  1. Define a dataclass with a boolean field
  2. Supply a list of arguments which does not include that field name
  3. 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sguggercommented, Jun 29, 2021

Yes, it does look like a bug. Fix is quite simple, I can make a PR.

0reactions
Craigacpcommented, Jun 29, 2021

Ok, I’ll check it tomorrow against our internal use case to make sure it fixes that too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found