Skip to content

Conversation

@TimMonko
Copy link
Member

@TimMonko TimMonko commented Dec 1, 2025

This pull request adds new features and improvements to the BatchRunner class and its usage in widgets, focusing on better progress tracking and error reporting. The main enhancements include support for an on_start callback to initialize progress bars, tracking the number of errors encountered during batch runs, and improved documentation and tests for these features.

Feature additions and API changes:

  • Added an on_start callback parameter to BatchRunner, which is called at the start of a batch with the total item count. This allows widgets to initialize progress bars more reliably. (src/nbatch/_runner.py, README.md) [1] [2] [3] [4]
  • Introduced an error_count property to BatchRunner to track the number of errors in the current or last batch, and reset it at the start of each run. (src/nbatch/_runner.py, README.md) [1] [2] [3] [4] [5] [6]

Widget integration improvements:

  • Updated the MyWidget example to use the new on_start callback for progress bar initialization and to display error counts after batch completion. (README.md) [1] [2] [3]

Documentation and usability:

  • Expanded documentation and usage examples to demonstrate passing additional arguments to the batch function and initializing progress bars with the new callback. (src/nbatch/_runner.py, README.md) [1] [2]

Testing improvements:

  • Added new tests for the on_start callback and error_count property, including tests for error tracking and reset behavior across runs. (tests/test_runner.py)

These changes make batch processing more robust and user-friendly, especially in GUI contexts where progress and error reporting are important.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the BatchRunner class by adding callback and error tracking capabilities. It introduces an on_start callback that receives the total item count when a batch begins (useful for initializing progress bars), and an error_count property that tracks errors with thread-safe access and automatic reset between runs. The implementation follows existing patterns in the codebase, with proper thread safety through lock usage and callbacks invoked outside locks to prevent deadlocks.

Key Changes:

  • Added on_start callback parameter to receive total item count at batch initialization
  • Introduced thread-safe error_count property with automatic reset on each run
  • Expanded documentation with usage examples for the new features

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/nbatch/_runner.py Added on_start callback parameter, error_count property with thread-safe access, and comprehensive documentation including usage examples
tests/test_runner.py Added tests for on_start callback invocation, error_count tracking, and reset behavior across multiple runs

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

----------
on_start : Callable[[int], None] | None, optional
Called when batch starts, receives total item count. Use to initialize
progress bars (e.g., ``on_start=lambda total: progress_bar.setMax(total)``).
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

Inconsistent method name in documentation. Line 47 uses setMax(total) but line 74 uses the correct setMaximum(total). Qt progress bars use setMaximum, not setMax. The example on line 47 should be updated to match line 74.

Suggested change
progress bars (e.g., ``on_start=lambda total: progress_bar.setMax(total)``).
progress bars (e.g., ``on_start=lambda total: progress_bar.setMaximum(total)``).

Copilot uses AI. Check for mistakes.
Comment on lines +57 to +67
def test_on_start_callback(self):
"""Test on_start callback is called with total count."""
on_start = MagicMock()

def process(item):
return item

runner = BatchRunner(on_start=on_start)
runner.run(process, [1, 2, 3, 4, 5], threaded=False)

on_start.assert_called_once_with(5)
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

The on_start callback is only tested in synchronous mode (threaded=False). For consistency with other callback tests (e.g., on_item_complete, on_complete, on_error in TestBatchRunnerNapariThreading), consider adding a test for on_start in threaded mode to ensure it works correctly in both execution paths.

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +80
def test_error_count_tracking(self):
"""Test error_count tracks errors during batch."""

def process(item):
if item in [2, 4]:
raise ValueError(f'Error on {item}')
return item

runner = BatchRunner()
runner.run(process, [1, 2, 3, 4, 5], threaded=False)

assert runner.error_count == 2
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

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

The error_count property is only tested in synchronous mode (threaded=False). For consistency with other error handling tests (e.g., test_run_napari_threaded_with_errors in TestBatchRunnerNapariThreading), consider adding a test for error_count in threaded mode to ensure the thread-safe counter works correctly across concurrent execution paths.

Copilot uses AI. Check for mistakes.
@TimMonko TimMonko merged commit a532e44 into ndev-kit:main Dec 1, 2025
14 checks passed
@TimMonko TimMonko deleted the feat/on-start branch December 1, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant