-
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 1 commit
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,15 +55,10 @@ | |
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
# These options have no dest and action is store_false in the | ||
# argparse and default is None. When parsing these options in a dict using | ||
# PipelineOptions,We either ignore/discard if these options are specified. | ||
# Defining a map with their dest would maintain consistency | ||
# across PipelineOptions(**dict), PipelineOptions.from_dictionary(dict), | ||
# and argparse. | ||
_STORE_FALSE_OPTIONS_WITH_DIFFERENT_DEST = { | ||
'use_public_ips': 'no_use_public_ips' | ||
} | ||
# 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): | ||
|
@@ -190,14 +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. For kwargs, | ||
please pass the option names instead of flag names. | ||
**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(). Passing flag names like | ||
{no_use_public_ips: True}, which is not defined to any | ||
destination(dest) in parser, would be discarded/ignored. | ||
Instead, pass the dest of the flag | ||
(dest of no_use_public_ips is use_public_ips), | ||
parser.add_argument() for each flag. Passing flags | ||
like {no_use_public_ips: True}, for which the flag name | ||
defined to a different destination(dest) in parser, | ||
would be discarded. Instead, pass the dest of | ||
the flag(dest of no_use_public_ips is use_public_ips), | ||
Eg: {use_public_ips: False} to get the desired behavior. | ||
|
||
""" | ||
|
@@ -231,15 +227,6 @@ def __init__(self, flags=None, **kwargs): | |
# Users access this dictionary store via __getattr__ / __setattr__ methods. | ||
self._all_options = kwargs | ||
|
||
if self.__class__ != PipelineOptions: | ||
_invalid_options = {} | ||
for option_name, option_value in self._all_options.items(): | ||
if option_name not in self._visible_option_list(): | ||
_invalid_options[option_name] = option_value | ||
|
||
if _invalid_options: | ||
_LOGGER.warning("Discarding invalid overrides: %s", _invalid_options) | ||
|
||
# Initialize values of keys defined by this class. | ||
for option_name in self._visible_option_list(): | ||
# Note that options specified in kwargs will not be overwritten. | ||
|
@@ -262,27 +249,25 @@ def from_dictionary(cls, options): | |
|
||
Returns: | ||
A PipelineOptions object representing the given arguments. | ||
|
||
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. | ||
""" | ||
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) | ||
# capture boolean flags with 3 values | ||
# {default=None, True, False} | ||
elif k in _STORE_FALSE_OPTIONS_WITH_DIFFERENT_DEST: | ||
_LOGGER.warning( | ||
"Instead of %s=%s, please provide %s=%s" % | ||
(k, v, _STORE_FALSE_OPTIONS_WITH_DIFFERENT_DEST[k], True)) | ||
flags.append('--%s' % _STORE_FALSE_OPTIONS_WITH_DIFFERENT_DEST[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)) | ||
|
@@ -397,14 +382,6 @@ def view_as(self, cls): | |
""" | ||
view = cls(self._flags) | ||
|
||
_invalid_options = {} | ||
for option_name, option_value in self._all_options.items(): | ||
if option_name not in self._visible_option_list(): | ||
_invalid_options[option_name] = option_value | ||
|
||
if _invalid_options: | ||
_LOGGER.warning("Discarding invalid overrides: %s", _invalid_options) | ||
|
||
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