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

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.

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: retain_unknown_options incorrectly assumes unknown arguments are consecutive

1 participant