[Python-ideas] Proposal: Use mypy syntax for function annotations

Steven D'Aprano steve at pearwood.info
Fri Aug 15 16:04:52 CEST 2014


On Fri, Aug 15, 2014 at 12:02:21PM +0200, Dennis Brakhane wrote:

> Unit testing your own code is the only way you can "guarantee" that your
> code runs correctly.

That is actually backwards. Unit testing cannot guarantee the code is 
correct. If you have 1000 tests, all that you have proved is that the 
code works for those 1000 cases. It tells you nothing about all the 
uncounted billions of other cases you haven't written tests for.

Given some set of tests which pass, you can be confident that your code 
gets at least those things correct (assuming the tests are themselves 
correct -- you write tests for your tests, don't you? *wink*). But 
that's all. As Dijkstra said, “Program testing can be used to show the 
presence of bugs, but never to show their absence.”

To put it another way, there could be a million bugs hiding in all the 
corners of your code that you don't test, and you can never test 
*everything*.

Static typing is complementary to unit testing. Static typing can 
eliminate a lot of the unit tests that you otherwise would have to 
write. For example, you might have unit tests that try to demonstrate 
that given arguments of type float, the function will return a float; 
given Fraction arguments, the function returns a Fraction; and so on. A 
static type checker can *prove* that[1] instead of merely testing it 
with a few dozen examples and hoping the result generalises to all the 
billions of other floats and Fractions not tested. Static typing is 
actually a form of automated correctness testing.




[1] Assuming you trust that the checker itself is bug free.

-- 
Steven


More information about the Python-ideas mailing list