-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[BEAM-13709] Inconsistent behavior when parsing boolean flags across different APIs in Python SDK #16929
Merged
Merged
[BEAM-13709] Inconsistent behavior when parsing boolean flags across different APIs in Python SDK #16929
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d122bf
Map to define relation between flags
AnandInguva 8ac7657
Add warnings to the discarded flags
AnandInguva 3af5a68
Fixup: lint
AnandInguva 10cff75
Fixup: docstring
AnandInguva eedce2f
Refactor docstring, unittest
AnandInguva cb24183
Update docstring
AnandInguva 078f58b
Change Map name and update comments
AnandInguva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,11 @@ | |
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
# Map the boolean option with the flag_name for the flags that have a | ||
# destination(dest) different from the flag name and the | ||
# default value is None in parser.add_argument(). | ||
_FLAGS_WITH_DIFFERENT_DEST = {'use_public_ips': 'no_use_public_ips'} | ||
|
||
|
||
def _static_value_provider_of(value_type): | ||
""""Helper function to plug a ValueProvider into argparse. | ||
|
@@ -180,7 +185,15 @@ def __init__(self, flags=None, **kwargs): | |
flags: An iterable of command line arguments to be used. If not specified | ||
then sys.argv will be used as input for parsing arguments. | ||
|
||
**kwargs: Add overrides for arguments passed in flags. | ||
**kwargs: Add overrides for arguments passed in flags. For overrides | ||
of arguments, please pass the `option names` instead of | ||
flag names. | ||
Option names: These are defined as dest in the | ||
parser.add_argument() for each flag. Passing flags | ||
like {no_use_public_ips: True}, for which the dest is | ||
defined to a different flag name in the parser, | ||
would be discarded. Instead, pass the dest of | ||
the flag (dest of no_use_public_ips is use_public_ips). | ||
""" | ||
# Initializing logging configuration in case the user did not set it up. | ||
logging.basicConfig() | ||
|
@@ -237,9 +250,22 @@ def from_dictionary(cls, options): | |
""" | ||
flags = [] | ||
for k, v in options.items(): | ||
# Note: If a boolean flag is True in the dictionary, | ||
# implicitly the method assumes the boolean flag is | ||
# specified as a command line argument. If the | ||
# boolean flag is False, this method simply discards them. | ||
# Eg: {no_auth: True} is similar to python your_file.py --no_auth | ||
# {no_auth: False} is similar to python your_file.py. | ||
if isinstance(v, bool): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if v: | ||
flags.append('--%s' % k) | ||
elif k in _FLAGS_WITH_DIFFERENT_DEST: | ||
# Capture overriding flags, which have a different dest | ||
# from the flag name defined in the parser.add_argument | ||
# Eg: no_use_public_ips, which has the dest=use_public_ips | ||
# different from flag name | ||
flag_that_disables_the_option = (_FLAGS_WITH_DIFFERENT_DEST[k]) | ||
flags.append('--%s' % flag_that_disables_the_option) | ||
elif isinstance(v, list): | ||
for i in v: | ||
flags.append('--%s=%s' % (k, i)) | ||
|
@@ -353,6 +379,7 @@ def view_as(self, cls): | |
|
||
""" | ||
view = cls(self._flags) | ||
|
||
for option_name in view._visible_option_list(): | ||
# Initialize values of keys defined by a cls. | ||
# | ||
|
This file contains 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
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.
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.
Consider the following for readability:
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.
Variable name sounds good. Changed the comments a little bit