[Python-ideas] Pre-conditions and post-conditions

Wes Turner wes.turner at gmail.com
Thu Aug 30 18:28:37 EDT 2018


def example_func(x, y):
    def __assert_before__(example_func):
        #implicit, AST-able assertion expressions

    # ... code

    def __assert_after__(example_func):
        #

    def __assert_after__invariants_02(example_func):
        # "

But these need to be composed / mixed in in MRO order and overridable; more
like a class with a __call__() and metaclassery for source-order
composition that has to do substring matches for e.g. __assert__*.

Blocks of expressions identified with keywords wouldn't be overrideable
('relaxed',); and would need to do fancy AST that replicates existing MRO?

On Thursday, August 30, 2018, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 08/30/2018 01:49 PM, Marko Ristin-Kaufmann wrote:
>
> classC(A):
>>      # C.some_func also inherits the contracts from A.
>>      #   It weakens the precondition:
>>      #       it operates either on sorted lists OR
>>      #           the lists that are shorter than 10 elements.
>>      #
>>      #  It strenghthens the postcondition:
>>      #       It needs to return an integer larger than
>>      #       the length of the input list AND
>>      #           the result needs to be divisible by 2.
>>      @icontract.post(lambdaresult: result %2==0)
>>      defsome_func(self, lst: List[int]) ->int:
>>          # ...
>>
>
> I think you forgot an @icontract.pre() here.
>
> --
> ~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180830/e69a8fba/attachment.html>


More information about the Python-ideas mailing list