Skip to content

Commit d841b8d

Browse files
committed
Use add_experiment() instead of experiments.append() in Python
Replaces manual list manipulation pattern with DebugOptions.add_experiment() which handles null-init and deduplication internally. Resolves #19347
1 parent 8b07c7a commit d841b8d

5 files changed

Lines changed: 6 additions & 25 deletions

File tree

sdks/python/apache_beam/io/external/xlang_parquetio_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def test_xlang_parquetio_write(self):
5151
address = 'localhost:%s' % port
5252
try:
5353
with TestPipeline() as p:
54-
p.get_pipeline_options().view_as(DebugOptions).experiments.append(
55-
'jar_packages=' + expansion_jar)
54+
p.get_pipeline_options().view_as(DebugOptions).add_experiment('jar_packages=' + expansion_jar)
5655
p.not_use_test_runner_api = True
5756
_ = p \
5857
| beam.Create([

sdks/python/apache_beam/io/iobase_test.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,7 @@ def test_try_split_with_any_exception(self):
196196
class UseSdfBoundedSourcesTests(unittest.TestCase):
197197
def _run_sdf_wrapper_pipeline(self, source, expected_values):
198198
with beam.Pipeline() as p:
199-
experiments = (p._options.view_as(DebugOptions).experiments or [])
200-
201-
# Setup experiment option to enable using SDFBoundedSourceWrapper
202-
if 'beam_fn_api' not in experiments:
203-
# Required so mocking below doesn't mock Create used in assert_that.
204-
experiments.append('beam_fn_api')
205-
206-
p._options.view_as(DebugOptions).experiments = experiments
199+
p._options.view_as(DebugOptions).add_experiment('beam_fn_api')
207200

208201
actual = p | beam.io.Read(source)
209202
assert_that(actual, equal_to(expected_values))

sdks/python/apache_beam/pipeline.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ def __init__(
232232
# set default experiments for portable runners
233233
# (needs to occur prior to pipeline construction)
234234
if runner.is_fnapi_compatible():
235-
experiments = (self._options.view_as(DebugOptions).experiments or [])
236-
if not 'beam_fn_api' in experiments:
237-
experiments.append('beam_fn_api')
238-
self._options.view_as(DebugOptions).experiments = experiments
235+
self._options.view_as(DebugOptions).add_experiment('beam_fn_api')
239236

240237
self.local_tempdir = tempfile.mkdtemp(prefix='beam-pipeline-temp')
241238

sdks/python/apache_beam/runners/dataflow/internal/apiclient.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,12 @@ def __init__(
147147
])
148148
# TODO: Use enumerated type instead of strings for job types.
149149
if job_type.startswith('FNAPI_'):
150-
self.debug_options.experiments = self.debug_options.experiments or []
151-
152-
debug_options_experiments = self.debug_options.experiments
153150
# Add use_multiple_sdk_containers flag if it's not already present. Do not
154151
# add the flag if 'no_use_multiple_sdk_containers' is present.
155152
# TODO: Cleanup use_multiple_sdk_containers once we deprecate Python SDK
156153
# till version 2.4.
157-
if ('use_multiple_sdk_containers' not in debug_options_experiments and
158-
'no_use_multiple_sdk_containers' not in debug_options_experiments):
159-
debug_options_experiments.append('use_multiple_sdk_containers')
154+
if ('no_use_multiple_sdk_containers' not in (self.debug_options.experiments or [])):
155+
self.debug_options.add_experiment('use_multiple_sdk_containers')
160156
# FlexRS
161157
if self.google_cloud_options.flexrs_goal == 'COST_OPTIMIZED':
162158
self.proto.flexResourceSchedulingGoal = (

sdks/python/apache_beam/runners/portability/fn_api_runner/fn_runner.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ def run_pipeline(
141141
RuntimeValueProvider.set_runtime_options({})
142142

143143
# Setup "beam_fn_api" experiment options if lacked.
144-
experiments = (
145-
options.view_as(pipeline_options.DebugOptions).experiments or [])
146-
if not 'beam_fn_api' in experiments:
147-
experiments.append('beam_fn_api')
148-
options.view_as(pipeline_options.DebugOptions).experiments = experiments
144+
options.view_as(pipeline_options.DebugOptions).add_experiment('beam_fn_api')
149145

150146
# This is sometimes needed if type checking is disabled
151147
# to enforce that the inputs (and outputs) of GroupByKey operations

0 commit comments

Comments
 (0)