Skip to content

Commit bb1b2ab

Browse files
authored
add partials vs kwargs test for clarity (#4)
This pull request adds a new test to demonstrate and recommend using keyword arguments (kwargs) as a cleaner alternative to functools.partial when passing extra arguments to batch processing functions. This helps clarify best practices for writing batch tests and ensures both approaches produce identical results. **Testing improvements:** * Added `test_kwargs_replaces_partial` to `tests/test_runner.py` to demonstrate and recommend using kwargs instead of `functools.partial` for passing extra arguments to batch functions. The test confirms that both approaches yield the same results, but highlights kwargs as the cleaner pattern.
1 parent a532e44 commit bb1b2ab

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/test_runner.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,47 @@ def process(item, multiplier, suffix=''):
148148

149149
assert results == ['10!', '20!', '30!']
150150

151+
def test_kwargs_replaces_partial(self):
152+
"""Test kwargs pattern as alternative to functools.partial.
153+
154+
This demonstrates the recommended pattern for passing extra arguments
155+
to batch functions without using functools.partial.
156+
"""
157+
from functools import partial
158+
159+
results_kwargs = []
160+
results_partial = []
161+
162+
def process_item(item, output_dir, sigma=1.0):
163+
"""Simulate processing with configurable parameters."""
164+
return f'{item}_sigma{sigma}_to_{output_dir}'
165+
166+
# Using kwargs (recommended pattern - cleaner!)
167+
runner1 = BatchRunner(
168+
on_item_complete=lambda r, ctx: results_kwargs.append(r),
169+
)
170+
runner1.run(
171+
process_item,
172+
['img1', 'img2'],
173+
output_dir='output',
174+
sigma=2.5,
175+
threaded=False,
176+
)
177+
178+
# Using partial (old pattern - still works but more verbose)
179+
runner2 = BatchRunner(
180+
on_item_complete=lambda r, ctx: results_partial.append(r),
181+
)
182+
process_func = partial(process_item, output_dir='output', sigma=2.5)
183+
runner2.run(process_func, ['img1', 'img2'], threaded=False)
184+
185+
# Both approaches produce identical results
186+
assert results_kwargs == results_partial
187+
assert results_kwargs == [
188+
'img1_sigma2.5_to_output',
189+
'img2_sigma2.5_to_output',
190+
]
191+
151192
def test_run_sync_context_values(self):
152193
"""Test BatchContext values in callbacks."""
153194
contexts = []

0 commit comments

Comments
 (0)