Skip to content

fix: wrap async mock factory errors in MockFactoryError and separate arity check (#79) - #88

Merged
Bbenchaya merged 1 commit into
intuit:mainfrom
Fire162:bugfix/79-async-mock-factory-error
Oct 5, 2026
Merged

Bbenchaya merged 1 commit into
intuit:mainfrom
Fire162:bugfix/79-async-mock-factory-error

Conversation

@Fire162

@Fire162 Fire162 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes two issues with mock factory resolution identified in #79:

  1. Async mock factories bypassing MockFactoryError: In wrapper.py, coroutines returned by async mock factories were awaited inside the mock execution block. If the async factory raised an exception during resolution, strict mode raised raw exceptions instead of MockFactoryError, and lenient mode did not format the failure message through MockFactoryError. Awaiting the coroutine is now handled during resolution, wrapping exceptions with raise MockFactoryError(tool_name, e) from e.
  2. 2-argument factory TypeError masking: MockToolsRegistry.resolve() and validate_mock_signature() previously called mock_fn(scenario_metadata, config) inside the same try/except TypeError used to test 2-argument signature compatibility with sig.bind({}, {}). If a 2-argument factory raised a TypeError from its own body, it triggered a fallback to 1-argument invocation, causing a misleading arity error to mask the real exception. The arity determination via sig.bind() is now performed before calling mock_fn.

Testing

  • Added test_two_arg_factory_type_error_cause_preserved in tests/test_mocking.py.
  • Added test_two_arg_factory_type_error_message_preserved in tests/test_validation.py.
  • Updated test_async_factory_error_reraises_in_strict_mode in tests/test_wrapper.py.
  • Added TestAsyncMockFactoryErrors test suite in tests/test_wrapper.py.
  • Ran unit tests (uv run pytest), e2e tests (uv run pytest tests-e2e/), linter (uv run ruff check .), formatter check (uv run ruff format --check .), and type checker (uv run mypy src/). All passed.

Fixes #79

…ity check

- In wrapper.py, await async mock factory coroutines during resolution and wrap exceptions in MockFactoryError so async factories follow the same handling path as sync factories.
- In mock_registry.py and validation.py, determine mock factory arity using sig.bind() before calling the factory function to avoid catching factory-internal TypeErrors as arity mismatches.
- Add unit tests for 2-arg factory TypeError reporting and an async twin of TestMockFactoryErrors.

Fixes intuit#79
@Bbenchaya
Bbenchaya merged commit 4f8d64c into intuit:main Oct 5, 2026
7 checks passed

This branch was successfully deployed

1 active deployment
pypi — d79347d7 Deployed Oct 5, 2026 by Bbenchaya via Tag & Publish #50
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.

bug: async mock factories bypass MockFactoryError; TypeError inside a 2-arg factory is misreported by resolve()

2 participants