Skip to content

[Python] Only give an unknown option a value that directly follows it - #40647

Open
Thanatos9404 wants to merge 1 commit into
apache:masterfrom
Thanatos9404:fix-22434-retain-unknown-options
Open

Thanatos9404 wants to merge 1 commit into
apache:masterfrom
Thanatos9404:fix-22434-retain-unknown-options

Conversation

@Thanatos9404

@Thanatos9404 Thanatos9404 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #22434

PipelineOptions.get_all_options(retain_unknown_options=True) 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 entries that are next to each other there aren't always next to each other in the original flags. For --unknown_flag --job_name=foo bar, --unknown_flag was registered as taking a value and parsing failed with expected one argument, which also broke Pipeline.run().

Now an unknown flag only takes a value when the value directly follows it in the original flags. Cases that already worked behave as before. One related case changes: --flag=value followed by a stray argument now keeps its inline value (the input that broke the load tests in #22422).

Tests: new cases in pipeline_options_test.py, six of which fail without the change and pass with it. I also added a CHANGES.md entry under Bugfixes for 2.78.0.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR fixes a bug in PipelineOptions.get_all_options(retain_unknown_options=True) where non-adjacent unknown arguments were erroneously paired together after known arguments were stripped by argparse. By tracking the original indices of unknown arguments in the raw flag list, it ensures an unknown flag only consumes a value that directly follows it and properly handles --flag=value options followed by stray positional arguments.

🔍 General Feedback

  • The implementation in _find_positions and get_all_options is clean, concise, and handles edge cases (like -- separators, single-dash options, inline = values, and known positionals) accurately.
  • The test coverage added in pipeline_options_test.py is thorough and directly verifies all relevant flag combinations.
  • One minor edge case to be aware of (not introduced by this PR, as self._flags is already re-parsed multiple times): if a caller passes a single-use iterator (e.g., a generator) as flags to PipelineOptions(flags), it would be exhausted during __init__. Normalizing self._flags to a list in __init__ could be a nice future hardening, though out of scope for this fix.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

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).

@github-actions
github-actions Bot requested a review from claudevdm October 18, 2026 18:16
@Thanatos9404
Thanatos9404 force-pushed the fix-22434-retain-unknown-options branch 2 times, most recently from 6bd5f33 to ff664f7 Compare October 23, 2026 19:01
@github-actions github-actions Bot added the size/m M: 50-249 lines changed label Oct 23, 2026
@Thanatos9404
Thanatos9404 force-pushed the fix-22434-retain-unknown-options branch from ff664f7 to fddb45b Compare October 26, 2026 18:50
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.09%. Comparing base (12bbb17) to head (5659c17).
⚠️ Report is 18 commits behind head on master.

Files with missing lines Patch % Lines
...dks/python/apache_beam/options/pipeline_options.py 82.35% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #40647      +/-   ##
==========================================
+ Coverage   79.83%   80.09%   +0.26%     
==========================================
  Files         494      494              
  Lines       83575    83617      +42     
==========================================
+ Hits        66722    66975     +253     
+ Misses      16853    16642     -211     
Flag Coverage Δ
python 80.09% <82.35%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@Thanatos9404
Thanatos9404 force-pushed the fix-22434-retain-unknown-options branch 2 times, most recently from c428efc to a035011 Compare October 29, 2026 22:49
With retain_unknown_options, get_all_options registers each unknown flag
with argparse and parses the flags again. Whether an unknown flag takes a
value was decided from the list of unknown args alone, but those are not
necessarily adjacent in the original flags. For

  --unknown_flag --job_name=foo bar

the unknown args are ['--unknown_flag', 'bar'], so --unknown_flag was
registered as taking a value, and parsing the original flags again failed
with "expected one argument". This also made Pipeline.run() fail.

Look up where each unknown arg came from in the original flags and only
treat an unknown flag as taking a value when that value directly follows
it there. A flag written as --flag=value always uses its inline value, so
it is no longer registered under the name "--flag=value" when a stray
argument follows it.

Fixes apache#22434
@Thanatos9404
Thanatos9404 force-pushed the fix-22434-retain-unknown-options branch from a035011 to 5659c17 Compare October 31, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1 participant