This is an automated email from the ASF dual-hosted git repository.
derrickaw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new 25cb8b76a96 fix(beam): guard jinja_variable_flags collision with
pipeline options (#39888)
25cb8b76a96 is described below
commit 25cb8b76a96b735ef83cd9ecc7c28aa7c3f9bde1
Author: Jeremy Schoemaker <[email protected]>
AuthorDate: Sat Aug 29 09:05:52 2026 -0500
fix(beam): guard jinja_variable_flags collision with pipeline options
(#39888)
* fix(beam): guard jinja_variable_flags collision with pipeline options
Fix verified RED->GREEN. Yaml jinja variable exact-name collision steals
pipeline option at main.py:64
* test(beam): cover jinja_variable_flags pipeline option collision
* fix(beam): apply yapf formatting to main_test.py
---
sdks/python/apache_beam/yaml/main.py | 10 ++++++++++
sdks/python/apache_beam/yaml/main_test.py | 14 ++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/sdks/python/apache_beam/yaml/main.py
b/sdks/python/apache_beam/yaml/main.py
index 804798b82e0..4a36c9da59e 100644
--- a/sdks/python/apache_beam/yaml/main.py
+++ b/sdks/python/apache_beam/yaml/main.py
@@ -61,7 +61,17 @@ def _preparse_jinja_flags(argv):
return argv
jinja_variable_parser = argparse.ArgumentParser(allow_abbrev=False)
+ # Guard against jinja_variable_flags colliding with pipeline options.
+ # If a flag collides with a known pipeline option, skip it and require
+ # the variable to be provided via --jinja_variables JSON instead.
+ try:
+ from apache_beam.options.pipeline_options import PipelineOptions
+ _pipeline_option_names = set(PipelineOptions([]).get_all_options().keys())
+ except Exception:
+ _pipeline_option_names = set()
for flag_name in jinja_args.jinja_variable_flags:
+ if flag_name.replace('-', '_') in _pipeline_option_names:
+ continue
jinja_variable_parser.add_argument('--' + flag_name)
jinja_flag_variables, pipeline_args = jinja_variable_parser.parse_known_args(
other_args)
diff --git a/sdks/python/apache_beam/yaml/main_test.py
b/sdks/python/apache_beam/yaml/main_test.py
index 43b8caa1853..3ef4feef11d 100644
--- a/sdks/python/apache_beam/yaml/main_test.py
+++ b/sdks/python/apache_beam/yaml/main_test.py
@@ -145,6 +145,20 @@ class MainTest(unittest.TestCase):
'pos_arg',
])
+ def test_preparse_jinja_flags_pipeline_option_collision(self):
+ # A jinja_variable_flags entry that collides with a known pipeline
+ # option (e.g. runner) must not swallow the pipeline flag.
+ argv = [
+ '--jinja_variable_flags=runner,var',
+ '--runner=DirectRunner',
+ '--var=my_line',
+ ]
+ self.assertCountEqual(
+ main._preparse_jinja_flags(argv), [
+ '--runner=DirectRunner',
+ '--jinja_variables=' + '{"var": "my_line"}',
+ ])
+
def test_jinja_datetime(self):
with tempfile.TemporaryDirectory() as tmpdir:
out_path = os.path.join(tmpdir, 'out.txt')