Python Sanity Proposal: Type Hinting Solution

Steven D'Aprano steve+comp.lang.python at
Fri Jan 23 06:59:36 CET 2015

Terry Reedy wrote:

> On 1/22/2015 10:59 PM, Chris Angelico wrote:
>> On Fri, Jan 23, 2015 at 2:22 PM, Terry Reedy <tjreedy at> wrote:
>>> This idea is so brilliant that it is already an option in mypy and is
>>> part
>>> of the new type-hint proposal.  The separate type-hint files are called
>>> 'stub files'.
>> It's worth pointing out, too, that the idea isn't panaceaic - it's
>> just another tool in the box. Any time you break related things into
>> separate places, especially separate files, the tendency for them to
>> get out of sync grows dramatically.
> The same could be said of putting tests in a separate file.

I agree!

Python has doctests, which live right there with the function. It also has
assertions, which can be considered a form of continuous testing.

Eiffel has syntax for design-by-contract (testing pre-conditions,
post-conditions and invariants) there in the body of the function or

Contracts are a form of testing, since they are designed to be disabled in
production. Other languages with syntactic support for Design By Contract
include Clojure, Cobra, D, Mercury, Perl6 and Racket.

Cobra is especially close to Python-like syntax, and supports unit tests as

    def sqroot(i as int) as float
           i > 0
           result > 0
           assert sqroot(25) == 5.0

It would be nice to be able to include at least *some* tests right there in
the code rather than in a separate file.


More information about the Python-list mailing list