Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

martinResearch
Copy link

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.

@@ -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
Copy link
Contributor

@jcal-15 jcal-15 Dec 10, 2024

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

Comment on lines +64 to +67
params = argpument_parser.parse_args([])
print(params)
self.assertEqual(42, params.x)
self.assertEqual(False, params.y)
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants