On 8/29/2018 6:39 PM, Eric Fahlgren wrote:
On Wed, Aug 29, 2018 at 3:07 PM Marko Ristin-Kaufmann <marko.ris...@gmail.com <mailto:marko.ris...@gmail.com>> wrote:

    Could you please elaborate a bit? I don't see how the annotations
    would make the contracts invoked on inheritance. Consider this
    simple case:

    class A:
    @icontract.pre(lambda x: x > 0)
    def some_method(self, x: int)->None:
    pass

    class B(A):
    # Precondition should be inherited here.
    def some_method(self, x: int) -> None:
    pass

    You would still need to somehow decorate manually the overridden
    methods even though you would not specify any new contracts, right?
    Is there a mechanism in Python that I am not aware of that would
    allow us to accomplish that?


A metaclass does this pretty easily.  I have a thing I wrote years ago called MIS (Multi-Inheritance Safety) that is used to ensure you don't do anything stupid in our physics-modeling database.  The database is a collection of ~200 Python classes/mixins all deriving madly from each other to get various behaviors (Has mass? You'll need this.  Has moments?  You'll need this.  Has physical extent, i.e., can be seen? You'll need these...).

I think a metaclass is a non-starter. If one were used, it would preclude using contracts in any case where a metaclass were already used, or where one was needed in the future. I'm sure people will disagree with me on this.

But, I think a more productive line of thinking is: what could be added to the language that would let contracts be implementable, and could also be used for other things, too? Sort of like how PEP 487 adds customizability that has wide applicability.

Eric
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to