You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "Thanatos9404 (via GitHub)" <gi...@apache.org> on 2026/10/18 17:41:03 UTC

[PR] [Python] Only give an unknown option a value that directly follows it [beam]

Thanatos9404 opened a new pull request, #40647:
URL: https://github.com/apache/beam/pull/40647

   Fixes #22434
   
   `PipelineOptions.get_all_options(retain_unknown_options=True)` registers each unknown flag with argparse and then parses the original flags again. It decided whether an unknown flag takes a value by looking at the next entry in the list of unknown args. argparse takes the known options out of that list, so two entries that are next to each other there are not always next to each other in the original flags.
   
   For `--unknown_flag --job_name=foo bar` the unknown args are `['--unknown_flag', 'bar']`. `--unknown_flag` was registered as taking a string, and the second parse failed with `argument --unknown_flag: expected one argument`. `Pipeline.run()` calls `get_all_options(retain_unknown_options=True)`, so the pipeline failed as well.
   
   This change looks up where each unknown arg sits in the original flags, and only treats an unknown flag as taking a value when the value directly follows it there. Cases that already worked behave as before:
   
   - `--flag value` and `--flag=value` are kept as strings, `--flag` on its own as `True`
   - a value used by a known option or positional is not taken by an unknown flag
   - single dash flags that take a value are still dropped with a warning
   - parsing still stops at `--`
   
   One related case also changes: `--flag=value` followed by a stray argument now keeps its inline value. Before, the whole `--flag=value` token was registered as the flag name and given the stray argument (the input that broke the load tests in #22422).
   
   Tests: added cases to `pipeline_options_test.py` for the example in the issue, an unknown flag before a known option with a separate value, unknown and known flags mixed, `--flag=value` followed by a stray value, repeated unary and repeated valued flags, `--`, a single dash flag, and a known positional. Six of them fail without the change. Locally:
   
   - `pytest apache_beam/options/`: 247 passed, 14 skipped
   - `pytest apache_beam/pipeline_test.py apache_beam/transforms/display_test.py`: 72 passed, 2 skipped
   - `pytest apache_beam/runners/portability/portable_runner_test.py -k option`: 5 passed, 2 skipped
   - `yapf --diff` (0.43.0), `ruff check` (0.15.22) and `pyrefly check` (1.1.1) on the changed files: clean
   
   I updated `CHANGES.md` under Bugfixes for 2.78.0.
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
    - [x] Replace these instructions with a description of your change and why you have made it.
    - [x] Mention the appropriate issue in your description (for example: `addresses #123` or `fixes #456`).
    - [x] Update `CHANGES.md` with noteworthy changes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "Thanatos9404 (via GitHub) via github" <gi...@beam.apache.org>.
Thanatos9404 commented on code in PR #40647:
URL: https://github.com/apache/beam/pull/40647#discussion_r4527798073


##########
sdks/python/apache_beam/options/pipeline_options.py:
##########
@@ -91,6 +92,26 @@ def _f(value):
   return _f
 
 
+def _find_positions(args, flags):

Review Comment:
   Added in 5d8aaa8e58.



##########
sdks/python/apache_beam/options/pipeline_options.py:
##########
@@ -545,7 +573,12 @@ def add_new_arg(arg, **kwargs):
         if not unknown_args[i].startswith('-'):
           i += 1
           continue
-        if i + 1 >= len(unknown_args) or unknown_args[i + 1].startswith('-'):
+        has_value = (
+            '=' not in unknown_args[i] and i + 1 < len(unknown_args) and
+            not unknown_args[i + 1].startswith('-') and
+            positions[i] is not None and positions[i] + 1 < len(all_flags) and
+            not all_flags[positions[i] + 1].startswith('-'))

Review Comment:
   Done in 5d8aaa8e58. The check now lives in `_has_separate_value`, with a docstring that explains why the value has to directly follow the flag in the original flags. Behavior is unchanged and the existing unknown option tests still pass.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40647:
URL: https://github.com/apache/beam/pull/40647#issuecomment-6253408186

   Assigning reviewers:
   
   R: @claudevdm for label python.
   Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)
   
   
   Note: If you would like to opt out of this review, comment `assign to next reviewer`.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #40647:
URL: https://github.com/apache/beam/pull/40647#issuecomment-6425285515

   ## [Codecov](https://app.codecov.io/gh/apache/beam/pull/40647?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `82.35294%` with `3 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 79.91%. Comparing base ([`2213e54`](https://app.codecov.io/gh/apache/beam/commit/2213e54bb58852c37eeda8eb572354076508ff2f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`fddb45b`](https://app.codecov.io/gh/apache/beam/commit/fddb45b8794f71d568ab8b745d835c4207b2859d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 27 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/beam/pull/40647?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...dks/python/apache\_beam/options/pipeline\_options.py](https://app.codecov.io/gh/apache/beam/pull/40647?src=pr&el=tree&filepath=sdks%2Fpython%2Fapache_beam%2Foptions%2Fpipeline_options.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vb3B0aW9ucy9waXBlbGluZV9vcHRpb25zLnB5) | 82.35% | [3 Missing :warning: ](https://app.codecov.io/gh/apache/beam/pull/40647?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff             @@
   ##           master   #40647       +/-   ##
   ===========================================
   + Coverage        0   79.91%   +79.91%     
   ===========================================
     Files           0      493      +493     
     Lines           0    83495    +83495     
   ===========================================
   + Hits            0    66721    +66721     
   - Misses          0    16774    +16774     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/beam/pull/40647/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [python](https://app.codecov.io/gh/apache/beam/pull/40647/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `79.91% <82.35%> (?)` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/beam/pull/40647?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "jrmccluskey (via GitHub) via github" <gi...@beam.apache.org>.
jrmccluskey commented on code in PR #40647:
URL: https://github.com/apache/beam/pull/40647#discussion_r4527569366


##########
sdks/python/apache_beam/options/pipeline_options.py:
##########
@@ -91,6 +92,26 @@ def _f(value):
   return _f
 
 
+def _find_positions(args, flags):

Review Comment:
   Nit: add type annotations 



##########
sdks/python/apache_beam/options/pipeline_options.py:
##########
@@ -545,7 +573,12 @@ def add_new_arg(arg, **kwargs):
         if not unknown_args[i].startswith('-'):
           i += 1
           continue
-        if i + 1 >= len(unknown_args) or unknown_args[i + 1].startswith('-'):
+        has_value = (
+            '=' not in unknown_args[i] and i + 1 < len(unknown_args) and
+            not unknown_args[i + 1].startswith('-') and
+            positions[i] is not None and positions[i] + 1 < len(all_flags) and
+            not all_flags[positions[i] + 1].startswith('-'))

Review Comment:
   This boolean evaluation block is somewhat unwieldy to follow, I would suggest splitting it out into a helper + adding a docstring just for readability purposes. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "jrmccluskey (via GitHub) via github" <gi...@beam.apache.org>.
jrmccluskey merged PR #40647:
URL: https://github.com/apache/beam/pull/40647


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Python] Only give an unknown option a value that directly follows it [beam]

Posted by "Thanatos9404 (via GitHub) via github" <gi...@beam.apache.org>.
Thanatos9404 commented on PR #40647:
URL: https://github.com/apache/beam/pull/40647#issuecomment-6772600920

   @jrmccluskey could you take a look when you have a moment? It has been waiting for review since mid-October. The latest commit covers the three lines Codecov flagged: the `sys.argv` path used when no flags are passed, and an arg that `_find_positions` does not find.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org