Support ArgumentDefaultsHelpFormatter by allowing extra calls to default_factory#70
Open
cdleonard wants to merge 7 commits intomivade:mainfrom
Open
Support ArgumentDefaultsHelpFormatter by allowing extra calls to default_factory#70cdleonard wants to merge 7 commits intomivade:mainfrom
cdleonard wants to merge 7 commits intomivade:mainfrom
Conversation
Still check that default_factory takes effect called at parsing time, but allow additional calls to default_factory in order to initialize argparse option default This breaks if default_factory has side-effects but that is an extremely bad idea. It's much more useful to support ArgumentDefaultsHelpFormatter instead.
…elds with dataclass-level defaults This prevents argparse from assigning the argparse-level default. This ensures that any default_factory is always called at parsing time instead of parser-creation time.
This is an explicit test for the expected behavior in issue mivade#32
…t_factory treatment
…ult_factory behavior change
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.