And 4) DBC constraints also become more useful than standard assertions when used in the context of a class hierarchy.

I.e. preconditions, postconditions and invariants should be stronger (or sometimes weaker, IIRC, there are subtle rules related to variance and contravariance) for subclasses than for superclasses.

This is another subject that should be dealt with if we want the true power of DBC in Python.

  S.

On Fri, Sep 10, 2021 at 9:26 AM Stéfane Fermigier <sf@fermigier.com> wrote:


On Fri, Sep 10, 2021 at 7:30 AM Steven D'Aprano <steve@pearwood.info> wrote:

[...] If we are seriously considering DBC for Python, [...]

As I think I wrote in the previous thread about DBC in Python, there are already several DBC libraries on PyPI and we should precisely evaluate both their design and impact (e.g. usage).

One of the most recent / best designed ones seems to be Deal by Orsinium: https://github.com/life4/deal

The Wikipedia page for DBC lists a few others: "Python, using packages like deal, icontract, PyContracts, Decontractors, dpcontracts, zope.interface, PyDBC or Contracts for Python. A permanent change to Python to support design by contracts was proposed in PEP-316, but deferred."

here's is my initial
proposal for function and method contracts. I don't have a proposal for
class invariants as yet.

Deal (and similar, older, libraries)  addresses similar needs, within the context of the existing Python syntax.

So yes, syntax can be improved. But :

1) Do people (at least a significant part) want DBC in their Python code. Is DBC still a popular idea in the software industry ? 

(My 2 cts: I don't believe so).

2) Is one of the existing approach (i.e. through a library) enough for these persons, or do we really need to change the language ?

(I don't know, we need people who actually do DBC with existing libraries to tell us).

3) I think there is more to DBC that simply checking preconditions and postconditions and invariants at runtime, just as typechecking Python is a bit more work than sprinkling your code with "assert isinstance(something, SomeClass)".

Just as for type systems, DBC becomes really useful when you have a theorem prover that can prove (or refute) the consistency of your DBC constraints.  This is the hard part (IMHO syntax is just the cherry on the cake).

Cheers,

  S.

--
Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier - http://linkedin.com/in/sfermigier
Founder & CEO, Abilian - Enterprise Social Software - http://www.abilian.com/
Co-Founder & Co-Chairman, National Council for Free & Open Source Software (CNLL) - http://cnll.fr/
Co-Founder & Board Member, Association Professionnelle Européenne du Logiciel Libre (APELL) - https://www.apell.info/
Co-Founder & Spokesperson, European Cloud Industrial Alliance (EUCLIDIA) - https://www.euclidia.eu/ 
Founder & Organiser, PyParis & PyData Paris - http://pyparis.org/http://pydata.fr/


--
Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier - http://linkedin.com/in/sfermigier
Founder & CEO, Abilian - Enterprise Social Software - http://www.abilian.com/
Co-Founder & Co-Chairman, National Council for Free & Open Source Software (CNLL) - http://cnll.fr/
Co-Founder & Board Member, Association Professionnelle Européenne du Logiciel Libre (APELL) - https://www.apell.info/
Co-Founder & Spokesperson, European Cloud Industrial Alliance (EUCLIDIA) - https://www.euclidia.eu/ 
Founder & Organiser, PyParis & PyData Paris - http://pyparis.org/http://pydata.fr/