[ 
https://issues.apache.org/jira/browse/BEAM-6162?focusedWorklogId=171264&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-171264
 ]

ASF GitHub Bot logged work on BEAM-6162:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Dec/18 01:39
            Start Date: 01/Dec/18 01:39
    Worklog Time Spent: 10m 
      Work Description: charlesccychen closed pull request #7176: [BEAM-6162] 
Fix PipelineOptions argparse behavior
URL: https://github.com/apache/beam/pull/7176
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sdks/python/apache_beam/options/pipeline_options.py 
b/sdks/python/apache_beam/options/pipeline_options.py
index 4cd4f014ac07..308f367e9c6d 100644
--- a/sdks/python/apache_beam/options/pipeline_options.py
+++ b/sdks/python/apache_beam/options/pipeline_options.py
@@ -108,6 +108,14 @@ def add_value_provider_argument(self, *args, **kwargs):
     # have add_argument do most of the work
     self.add_argument(*args, **kwargs)
 
+  # The argparse package by default tries to autocomplete option names. This
+  # results in an "ambiguous option" error from argparse when an unknown option
+  # matching multiple known ones are used. This suppresses that behavior.
+  def error(self, message):
+    if message.startswith('ambiguous option: '):
+      return
+    super(_BeamArgumentParser, self).error(message)
+
 
 class PipelineOptions(HasDisplayData):
   """Pipeline options class used as container for command line options.
diff --git a/sdks/python/apache_beam/options/pipeline_options_test.py 
b/sdks/python/apache_beam/options/pipeline_options_test.py
index 659975363702..dac9b49a0c7a 100644
--- a/sdks/python/apache_beam/options/pipeline_options_test.py
+++ b/sdks/python/apache_beam/options/pipeline_options_test.py
@@ -25,6 +25,8 @@
 import hamcrest as hc
 
 from apache_beam.options.pipeline_options import PipelineOptions
+from apache_beam.options.pipeline_options import ProfilingOptions
+from apache_beam.options.pipeline_options import TypeOptions
 from apache_beam.options.value_provider import RuntimeValueProvider
 from apache_beam.options.value_provider import StaticValueProvider
 from apache_beam.transforms.display import DisplayData
@@ -298,6 +300,21 @@ def test_unknown_duplicate_args_converted_to_list(self):
     self.assertEqual(options.get_all_options()['dup_arg'],
                      ['val1', 'val2', 'val3'])
 
+  # The argparse package by default tries to autocomplete option names. This
+  # results in an "ambiguous option" error from argparse when an unknown option
+  # matching multiple known ones are used. This tests that we suppress this
+  # error.
+  def test_unknown_option_prefix(self):
+    # Test that the "ambiguous option" error is suppressed.
+    options = PipelineOptions(['--profi', 'val1'])
+    options.view_as(ProfilingOptions)
+
+    # Test that valid errors are not suppressed.
+    with self.assertRaises(SystemExit):
+      # Invalid option choice.
+      options = PipelineOptions(['--type_check_strictness', 'blahblah'])
+      options.view_as(TypeOptions)
+
 
 if __name__ == '__main__':
   logging.getLogger().setLevel(logging.INFO)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 171264)
    Time Spent: 0.5h  (was: 20m)

> Python PipelineOptions raises "ambiguous option" error due to argparse 
> behavior
> -------------------------------------------------------------------------------
>
>                 Key: BEAM-6162
>                 URL: https://issues.apache.org/jira/browse/BEAM-6162
>             Project: Beam
>          Issue Type: Bug
>          Components: sdk-py-core
>            Reporter: Charles Chen
>            Assignee: Charles Chen
>            Priority: Blocker
>             Fix For: 2.9.0
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> I am running an Jupyter notebook with an --profile option.  This works in 
> 2.8.0 but is broken after [https://github.com/apache/beam/pull/6847] as the 
> Python PipelineOptions raises "ambiguous option" error due to argparse 
> behavior of trying to autocomplete options by prefix (this behavior is due to 
> [https://bugs.python.org/issue29777] which the Python dev team does not want 
> to fix).



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to