-
Notifications
You must be signed in to change notification settings - Fork 15
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making add dataclass options public #60
base: main
Are you sure you want to change the base?
Making add dataclass options public #60
Conversation
@@ -359,7 +359,7 @@ def _add_dataclass_options( | |||
if field.default == field.default_factory == MISSING and not positional: | |||
kwargs["required"] = True | |||
else: | |||
kwargs["default"] = MISSING | |||
kwargs["default"] = field.default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to keep the default as MISSING
so that the dataclass's initialization handles the default value. This is needed so default_factory
can work correctly.
Also, doing it this way makes the initialization consistent with initializing the dataclass outside of the argparse_dataclass
package
params = argpument_parser.parse_args([]) | ||
print(params) | ||
self.assertEqual(42, params.x) | ||
self.assertEqual(False, params.y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parse_args
returns a namespace
object not a Opt
so it is not expected to have the defaults set.
draft PR to solve #58
I wrote this before realizing there was another PR to fix the same issue here
Note that I had to use
kwargs["default"] = field.default
for the test to pass. I did not read the code in enough detail to have confidence that it is the right fix.