Support ArgumentDefaultsHelpFormatter by allowing extra calls to default_factory #70
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes issues #32 and #65
ArgumentDefaultsHelpFormatter is currently broke because argparse_dataclass always passes a default value of
dataclasses.MISSINGto argparse. This is done in order to ensure that when default_factory is used it is only called exactly once at parse time.Relax this behavior to allow an additional call to default_factory at parser creation time so that defaults are properly visible. This can break when default_factory has side-effects but that would be a very bad practice.
It is still guaranteed that default_factory() is called for each new parse so something like
default_factory=dt.datetime.nowworks exactly as expected.This is implemented by initializing an argparse.Namespace before parsing which fills in all fields which have dataclass-level default values. This prevents argparse from assigning the argparse-level default and ensures that default_factory is called for a fresh result each parse.