-
Notifications
You must be signed in to change notification settings - Fork 18
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
Question/Feature request: Iterable usage in require
and snapshot
decorators
#249
Comments
If there really are features missing, I gladly offer my help and contribution by implementing those features! |
@claudio-ebel thanks for sharing your thoughts! It is indeed a missing feature. Some two years ago I spent quite some time thinking about it. However, what seems to work on some cases turns out to be inappropriate for other cases. For example, what if there are two iterators in the input and an iterator as the output and all these three somehow correlate? I came to the conclusion that iterators, which have side effects by their nature, are simply not a good fit for design-by-contract (similar to multi-threading). The complexity of many contracts in this context can not be captured by the simplistic logic of require/snapshot/ensure. Now enter behavioral sub-typing and inheritance and it becomes all very, very confusing :-). This does not mean that this shouldn't be covered by icontract; but I am still fuzzy on how to really approach the problem. Do you have a couple of practical examples where you wanted to impose contracts on iterators? Maybe let's start from those and see where that journey takes us. |
@mristin Thank you very much for your openness and this kind offer. I eagerly accept it! Indeed, I have a few practical examples. I am on vacation right now and have afterwords a stressful week at my job, but afterwords (July) I'd like to look at the project code and get myself used with the coding style and conventions therein. You prefer that I create a branch for this feature/issue, right? I'd create a few examples (as you suggested) there and maybe even a corresponding solution. Then it's all nicely separated… |
@claudio-ebel a separate branch would be great! Take your time! Let's first think about use cases and at the conceptual level -- coding is the easy part once that is all figured out :-). |
Question
How do I use
Iterable
in a@snapshot
or in a@require
decorator? I didn't find any hints in the documentation so I assume I either used them wrong or didn't understand a core concept. Otherwise, I would like to propose a feature request 😀Examples for
@ensure
If I try to take a snapshot of an
Iterable
, it is not possible sincesnapshot
consumes it. Using the fileiter_in_ensure.py
in a python environment where
icontract
andmore_itertools
is included, the error message is:showing that the
@snapshot
decorator already consumed the iterator, leaving an emptyresult
back. A possible workaround is to useitertools.tee
inside the function:but that requires to change the functions's signature for usage in
@ensure
only which – at least in my opinion – contradictsicontract
's philosophy.Examples for
@require
With the
@require
decorator, I even didn't find a workaround:results in
showing that
@require
already consumed the iterator and the functionrequire_iter
has no chance to access it again.Versions
Python 3.10.4
8.13.0
2.6.1
Feature pitch
If I didn't miss anything, there are features missing for the
@snapshot
and the@require
function decorators. I suggest to introduce additional arguments to disable iterator consumption.A possible example usage for
@snapshot
:A possible example usage for
@require
:The new argument
iter
indicates to use an independent iterator. In@require
's case, it forwards it to the decorated function.I'm aware that the proposed solution is not applicable to all iterables, but I'm still convinced it would pose an improvement.
The text was updated successfully, but these errors were encountered: