Python Sanity Proposal: Type Hinting Solution

Steven D'Aprano steve+comp.lang.python at pearwood.info
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 udel.edu> 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
method:

https://docs.eiffel.com/book/method/et-design-contract-tm-assertions-and-exceptions#Expressing_assertions


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
well:


    def sqroot(i as int) as float
        require 
           i > 0
        ensure
           result > 0
        tests
           assert sqroot(25) == 5.0
        body
            ...


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


-- 
Steven




More information about the Python-list mailing list