More debuggable UsageExpectation (bugfix)#2728
Conversation
This also makes the interface way less hackish, no longer relying on inspect at runtime (ough) but taking the current function as a paramter, the tradeoff is that the function call is more verbose but inspect is really a no-no + slow, so I think it is worth paything this price
Remove pointless test, it is unclear what this was testing, it is calling a random subset of the SA and monitoring an implementation detail of how usage is tracked? idk
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2728 +/- ##
==========================================
- Coverage 60.03% 59.97% -0.06%
==========================================
Files 487 487
Lines 48935 48969 +34
Branches 8765 8764 -1
==========================================
- Hits 29377 29369 -8
- Misses 18636 18682 +46
+ Partials 922 918 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR improves the debuggability of UsageExpectation failures by tracking and displaying a short history of which methods most recently modified the allowed-call set, and updates SessionAssistant to use the new API for modifying/enforcing expectations.
Changes:
- Refactors
UsageExpectationto store allowed calls by function name and adds modification-history tracking that is included inUnexpectedMethodCallmessages. - Updates
SessionAssistantto callenforce(self.<method>)and to mutate expectations viaallow(),disallow(), andallow_all(). - Updates developer tests to reflect the new exception messaging; removes a SessionAssistant test that previously asserted allowed-call transitions.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| checkbox-ng/plainbox/impl/developer.py | Refactors UsageExpectation enforcement/modification logic and enhances UnexpectedMethodCall messages with modification history. |
| checkbox-ng/plainbox/impl/session/assistant.py | Migrates SessionAssistant’s usage-expectation enforcement and allowed-call transitions to the new API. |
| checkbox-ng/plainbox/impl/test_developer.py | Updates unit tests to use the new enforcement API and to validate the updated error message format. |
| checkbox-ng/plainbox/impl/session/test_assistant.py | Removes the expected-call-sequence test that depended on the previous allowed_calls structure. |
Comments suppressed due to low confidence (1)
checkbox-ng/plainbox/impl/session/test_assistant.py:76
test_expected_call_sequencewas removed, which eliminates the only coverage in this module for SessionAssistant's expected-call state transitions. Given the refactor toUsageExpectation.allow/allow_all/disallow, it would be better to update the test to assert the new behavior (e.g., calling a method that should now be disallowed raisesUnexpectedMethodCalland the message/history are sensible).
def _get_mock_providers(self):
"""Get some mocked provides for testing."""
return [self.p1, self.p2, self.p3]
@mock.patch(
"plainbox.impl.session.assistant.UsageExpectation",
new=mock.MagicMock(),
)
@mock.patch("plainbox.impl.session.assistant._logger")
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fernando79513
left a comment
There was a problem hiding this comment.
Overall, great improvement, It took me a while to undestand it, but I can get it now, before it was too cryptic, XD
I have added a couple of comments, so we can maybe clarify it even more.
43d1e6f to
9ae657a
Compare
Description
Currently when we receive a UsageExpectation error in an issue it is really hard to know what went wrong. The reason is that the usage expectations are modified by any function but there is no way to know how we got to a given set. This PR starts tracking the modifications and includes them in the exception. Additionally the error now is clear about it being an error in Checkbox (the old error was weird for our users) and to report it to us.
On the implementation side, the old implementation compared the function code and had to jump through a few hoops to get the actual calling function and its code (given that it could have been decorated). This new implemtation removes all that and just uses the function name. This makes:
Resolved issues
N/A
Documentation
N/A
Tests
To test this remove some UsageExpectation from (for example)
_get_allowed_calls_in_normal_state. You will see the new error with a history of modifications.Here is a sample of the new error: