[Python-ideas] Pre-conditions and post-conditions
michael.lee.0x2a at gmail.com
Sat Sep 15 04:42:00 EDT 2018
I just want to point out that you don't need permission from anybody to
start a library. I think developing and popularizing a contracts library is
a reasonable goal -- but that's something you can start doing at any time
without waiting for consensus.
And if it gets popular enough, maybe it'll be added to the standard library
in some form. That's what happened with attrs, iirc -- it got fairly
popular and demonstrated there was an unfilled niche, and so Python
The contracts make merely tests obsolete that test that the function or
> class actually observes the contracts.
Is this actually the case? Your contracts are only checked when the
function is evaluated, so you'd still need to write that unit test that
confirms the function actually observes the contract. I don't think you
necessarily get to reduce the number of tests you'd need to write.
Please let me know what points *do not *convince you that Python needs
While I agree that contracts are a useful tool, I don't think they're going
to be necessarily useful for *all* Python programmers. For example,
contracts aren't particularly useful if you're writing fairly
straightforward code with relatively simple invariants.
I'm also not convinced that libraries where contracts are checked
specifically *at runtime* actually give you that much added power and
impact. For example, you still need to write a decent number of unit tests
to make sure your contracts are being upheld (unless you plan on checking
this by just deploying your code and letting it run, which seems
suboptimal). There's also no guarantee that your contracts will necessarily
be *accurate*. It's entirely possible that your
preconditions/postconditions might hold for every test case you can think
of, but end up failing when running in production due to some edge case
that you missed. (And if you decide to disable those pre/post conditions to
avoid the efficiency hit, you're back to square zero.)
Or I guess to put it another way -- it seems what all of these contract
libraries are doing is basically adding syntax to try and make adding
asserts in various places more ergonomic, and not much else. I agree those
kinds of libraries can be useful, but I don't think they're necessarily
useful enough to be part of the standard library or to be a technique
Python programmers should automatically use by default.
What might be interesting is somebody wrote a library that does something
more then just adding asserts. For example, one idea might be to try
hooking up a contracts library to hypothesis (or any other library that
does quickcheck-style testing). That might be a good way of partially
addressing the problems up above -- you write out your invariants, and a
testing library extracts that information and uses it to automatically
synthesize interesting test cases.
(And of course, what would be very cool is if the contracts could be
verified statically like you can do in languages like dafny -- that way,
you genuinely would be able to avoid writing many kinds of tests and could
have confidence your contracts are upheld. But I understanding implementing
such verifiers are extremely challenging and would probably have too-steep
of a learning curve to be usable by most people anyways.)
On Fri, Sep 14, 2018 at 11:51 PM, Marko Ristin-Kaufmann <
marko.ristin at gmail.com> wrote:
> Let me make a couple of practical examples from the work-in-progress (
> https://github.com/Parquery/pypackagery, branch mristin/initial-version)
> to illustrate again the usefulness of the contracts and why they are, in my
> opinion, superior to assertions and unit tests.
> What follows is a list of function signatures decorated with contracts
> from pypackagery library preceded by a human-readable description of the
> The invariants tell us what format to expect from the related string
> @icontract.inv(lambda self: self.name.strip() == self.name)
> @icontract.inv(lambda self: self.line.endswith("\n"))
> class Requirement:
> """Represent a requirement in requirements.txt."""
> def __init__(self, name: str, line: str) -> None:
> :param name: package name
> :param line: line in the requirements.txt file
> The postcondition tells us that the resulting map keys the values on their
> name property.
> @icontract.post(lambda result: all(val.name == key for key, val in result.items()))
> def parse_requirements(text: str, filename: str = '<unknown>') -> Mapping[str, Requirement]:
> Parse requirements file and return package name -> package requirement as in requirements.txt
> :param text: content of the ``requirements.txt``
> :param filename: where we got the ``requirements.txt`` from (URL or path)
> :return: name of the requirement (*i.e.* pip package) -> parsed requirement
> The postcondition ensures that the resulting list contains only unique
> elements. Mind that if you returned a set, the order would have been lost.
> @icontract.post(lambda result: len(result) == len(set(result)), enabled=icontract.SLOW)
> def missing_requirements(module_to_requirement: Mapping[str, str],
> requirements: Mapping[str, Requirement]) -> List[str]:
> List requirements from module_to_requirement missing in the ``requirements``.
> :param module_to_requirement: parsed ``module_to_requiremnt.tsv``
> :param requirements: parsed ``requirements.txt``
> :return: list of requirement names
> Here is a bit more complex example.
> - The precondition A requires that all the supplied relative paths
> (rel_paths) are indeed relative (as opposed to absolute).
> - The postcondition B ensures that the initial set of paths (given in
> rel_paths) is included in the results.
> - The postcondition C ensures that the requirements in the results are the
> subset of the given requirements.
> - The precondition D requires that there are no missing requirements (*i.e.
> *that each requirement in the given module_to_requirement is also defined
> in the given requirements).
> @icontract.pre(lambda rel_paths: all(rel_pth.root == "" for rel_pth in rel_paths)) # A
> lambda rel_paths, result: all(pth in result.rel_paths for pth in rel_paths),
> description="Initial relative paths included") # B
> lambda requirements, result: all(req.name in requirements for req in result.requirements),
> enabled=icontract.SLOW) # C
> lambda requirements, module_to_requirement: missing_requirements(module_to_requirement, requirements) == ,
> enabled=icontract.SLOW) # D
> def collect_dependency_graph(root_dir: pathlib.Path, rel_paths: List[pathlib.Path],
> requirements: Mapping[str, Requirement],
> module_to_requirement: Mapping[str, str]) -> Package:
> Collect the dependency graph of the initial set of python files from the code base.
> :param root_dir: root directory of the codebase such as "/home/marko/workspace/pqry/production/src/py"
> :param rel_paths: initial set of python files that we want to package. These paths are relative to root_dir.
> :param requirements: requirements of the whole code base, mapped by package name
> :param module_to_requirement: module to requirement correspondence of the whole code base
> :return: resolved depedendency graph including the given initial relative paths,
> I hope these examples convince you (at least a little bit :-)) that
> contracts are easier and clearer to write than asserts. As noted before in
> this thread, you can have the same *behavior* with asserts as long as you
> don't need to inherit the contracts. But the contract decorators make it
> very explicit what conditions should hold *without* having to look into
> the implementation. Moreover, it is very hard to ensure the postconditions
> with asserts as soon as you have a complex control flow since you would
> need to duplicate the assert at every return statement. (You could
> implement a context manager that ensures the postconditions, but a context
> manager is not more readable than decorators and you have to duplicate them
> as documentation in the docstring).
> In my view, contracts are also superior to many kinds of tests. As the
> contracts are *always* enforced, they also enforce the correctness
> throughout the program execution whereas the unit tests and doctests only
> cover a list of selected cases. Furthermore, writing the contracts in these
> examples as doctests or unit tests would escape the attention of most less
> experienced programmers which are not used to read unit tests as
> documentation. Finally, these unit tests would be much harder to read than
> the decorators (*e.g.*, the unit test would supply invalid arguments and
> then check for ValueError which is already a much more convoluted piece of
> code than the preconditions and postconditions as decorators. Such testing
> code also lives in a file separate from the original implementation making
> it much harder to locate and maintain).
> Mind that the contracts *do not* *replace* the unit tests or the
> doctests. The contracts make merely tests obsolete that test that the
> function or class actually observes the contracts. Design-by-contract helps
> you skip those tests and focus on the more complex ones that test the
> behavior. Another positive effect of the contracts is that they make your
> tests deeper: if you specified the contracts throughout the code base, a
> test of a function that calls other functions in its implementation will
> also make sure that all the contracts of that other functions hold. This
> can be difficult to implement with standard unit test frameworks.
> Another aspect of the design-by-contract, which is IMO ignored quite
> often, is the educational one. Contracts force the programmer to actually
> sit down and think *formally* about the inputs and the outputs
> (hopefully?) *before* she starts to implement a function. Since many
> schools use Python to teach programming (especially at high school level),
> I imagine writing contracts of a function to be a very good exercise in
> formal thinking for the students.
> Please let me know what points *do not *convince you that Python needs
> contracts (in whatever form -- be it as a standard library, be it as a
> language construct, be it as a widely adopted and collectively maintained
> third-party library). I would be very glad to address these points in my
> next message(s).
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas