New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
icontract fails to restore class invariants at method exit #247
Comments
Hi @maggyero , While I can't implement the feature, I'll describe the issue in the docs. Please let me know if you have an idea how to implement this. |
Hi @mristin, I used In order to properly deal with failures to fulfil a method postcondition, icontract should
In paragraph ‘Dealing with abnormal situations’, Meyer explains that exception handling method:
In paragraph ‘A disciplined exception-handling mechanism’, Meyer explains that to achieve it, Eiffel uses a
A @icontract.invariant(lambda self: self.x > 0)
class A:
def __init__(self):
self.x = 1
@icontract.rescue(lambda self: setattr(self, 'x', 1)) # performs self.x = 1 in case of exceptions
def f(self):
self.x = -1
raise ValueError I think the @icontract.invariant(lambda self: self.x > 0)
@icontract.rescue(lambda self: setattr(self, 'x', 1)) # performs self.x = 1 in case of exceptions
class A:
def __init__(self):
self.x = 1
def f(self):
self.x = -1
raise ValueError The only way to avoid introducing the
|
Hi @maggyero , I would opt that the
or
I foresee quite some complex logic there, so I wouldn't go with the simple lambdas. IMO, this will quickly lead to developing a parallel domain-specific language which is unnecessarily contrived. I'd rather stick with plain Python functions instead. Alternatively, the rescue instance method can also be supplied:
This is unfortunately not possible with a decorator, but need to come after the class definition. What do you think? What about inheritance? Would the rescue functions be stacked? Just out of curiosity: what is the scenario where you need this feature? I have never encountered it in practice myself as my exceptions are most often simply panics, but this is just my code style, of course. |
(I'm closing the issue due to inactivity.) |
icontract fails to restore class invariants at method exit:
This is contrary to what Bertrand Meyer prescribes in his 1992 seminal article Applying “Design by Contract”:
Class invariants are the minimal guarantees in case of failure.
The text was updated successfully, but these errors were encountered: