[Python] Deal Design by Contract to prove a Python script with decorators

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dockerized Deal that I can successfully run using

    docker run -it -v `pwd`:/scratch -w /scratch --rm deal python3 -m deal prove deal_demo_cat.py


    which produces

    deal_demo_cat.py
    cat
    proved! post-condition, post-condition, post-condition


    The code (copied from https://deal.readthedocs.io/basic/motivation.html) in deal_demo_cat.py is

    #!/usr/bin/env python3
    import deal

    @deal.ensure(lambda left, right, result: result.startswith(left))
    @deal.ensure(lambda left, right, result: result.endswith(right))
    @deal.ensure(lambda left, right, result: len(result) == len(left) + len(right))
    def cat(left: str, right: str) -> str:
    return left + right


    That works. However, when I write my function in add_one.py

    #!/usr/bin/env python3
    import deal

    @deal.pre(lambda number: number<1E100) # Precondition must be true before the function is executed.
    @deal.pre(lambda number: number>-1E100)
    @deal.ensure(lambda number, result: result==number+1)
    def add_one_to_arg(number: int | float) -> int | float:
    """
    do the addition
    """
    return number + 1


    and then run

    docker run -it -v `pwd`:/scratch -w /scratch --rm deal python3 -m deal prove add_one.py


    I get no output to the terminal. My question is why I am not getting any output. Is something missing from my script?

    For reproducibility, here's the Dockerfile used to containerize Deal:

    FROM phusion/baseimage:jammy-1.0.2@sha256:1584de70d2f34df8e2e21d2f59aa7b5ee75f3fd5e26c4f13155137b2d5478745

    # Use baseimage-docker's init system
    CMD ["/sbin/my_init"]

    # TODO: pin the apt package versions
    # Update and install packages
    RUN apt update && apt -y upgrade && apt -y install \
    python3 \
    python3-pip

    WORKDIR /opt/

    RUN python3 -m pip install --user 'deal[all]'

    # RUN python3 -m pip install deal-solver

    RUN python3 -m pip install pytest

    # https://github.com/timothycrosley/hypothesis-auto
    # https://timothycrosley.github.io/hypothesis-auto/
    RUN python3 -m pip install hypothesis-auto

    RUN python3 -m pip install mypy

    # as of April 2025 typeguard 4.2.2 is available, but the `CallMemo` feature isn't available after 4.0
    RUN python3 -m pip install typeguard==3.0.2


    and then I use

    docker build -f Dockerfile -t deal .

    Continue reading...

Compartilhe esta Página