Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
c9486fd0
Unverified
Commit
c9486fd0
authored
Jun 30, 2021
by
Sylvain Gugger
Committed by
GitHub
Jun 30, 2021
Browse files
Fix default bool in argparser (#12424)
* Fix default bool in argparser * Add more to test
parent
90d69456
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
3 deletions
+7
-3
src/transformers/hf_argparser.py
src/transformers/hf_argparser.py
+2
-2
tests/test_hf_argparser.py
tests/test_hf_argparser.py
+5
-1
No files found.
src/transformers/hf_argparser.py
View file @
c9486fd0
...
...
@@ -112,8 +112,8 @@ class HfArgumentParser(ArgumentParser):
# Hack because type=bool in argparse does not behave as we want.
kwargs
[
"type"
]
=
string_to_bool
if
field
.
type
is
bool
or
(
field
.
default
is
not
None
and
field
.
default
is
not
dataclasses
.
MISSING
):
# Default value is
Tru
e if we have no default when of type bool.
default
=
Tru
e
if
field
.
default
is
dataclasses
.
MISSING
else
field
.
default
# Default value is
Fals
e if we have no default when of type bool.
default
=
Fals
e
if
field
.
default
is
dataclasses
.
MISSING
else
field
.
default
# This is the value that will get picked if we don't include --field_name in any way
kwargs
[
"default"
]
=
default
# This tells argparse we accept 0 or 1 value after --field_name
...
...
tests/test_hf_argparser.py
View file @
c9486fd0
...
...
@@ -106,9 +106,13 @@ class HfArgumentParserTest(unittest.TestCase):
expected
.
add_argument
(
"--foo"
,
type
=
int
,
required
=
True
)
expected
.
add_argument
(
"--bar"
,
type
=
float
,
required
=
True
)
expected
.
add_argument
(
"--baz"
,
type
=
str
,
required
=
True
)
expected
.
add_argument
(
"--flag"
,
type
=
string_to_bool
,
default
=
Tru
e
,
const
=
True
,
nargs
=
"?"
)
expected
.
add_argument
(
"--flag"
,
type
=
string_to_bool
,
default
=
Fals
e
,
const
=
True
,
nargs
=
"?"
)
self
.
argparsersEqual
(
parser
,
expected
)
args
=
[
"--foo"
,
"1"
,
"--baz"
,
"quux"
,
"--bar"
,
"0.5"
]
(
example
,)
=
parser
.
parse_args_into_dataclasses
(
args
,
look_for_args_file
=
False
)
self
.
assertFalse
(
example
.
flag
)
def
test_with_default
(
self
):
parser
=
HfArgumentParser
(
WithDefaultExample
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment