[Python-ideas] Optional Static Typing -- the Python Way

Steven D'Aprano steve at pearwood.info
Sun Aug 24 14:41:39 CEST 2014


On Sat, Aug 23, 2014 at 08:37:34PM -0400, Cem Karan wrote:

> Going slightly sideways on this -- I think this is why we should use 
> decorators instead of annotations; as has already been mentioned twice 
> on the list, 
> http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ does a 
> good job pointing out that static and dynamic typing systems are 
> separate but overlapping concepts.  It also points out that both have 
> their place.  If we define decorators that can be turned on or off 
> easily (command-line option?  Environment variable? other?), then the 
> end user can choose if if her or she is going to do static, dynamic, 
> both, or none.  This could be useful when trying to track down that 
> annoying bug in long-running production code.

This applies equally to annotations, and in fact that's exactly what 
Mypy already does. You can run Mypy to do static type checking, or not 
run it, and the annotations will be ignored.

E.g. given a module program.py, you can:

# Type check and then run the program:
mypy program.py

# Just run it, with no extra type checks:
python3 program.py


Guido's proposal is *not* to add static types to the CPython 
interpreter, at least not yet. It is just to standardise on annotations 
for type hinting, agree on a syntax for those type hints, and then allow 
third-part tools (linters, editors, IDEs, etc.) and alternative 
interpreters (like mypy) to actually use the type hints.

Looking forward to the distant future, if CPython gains its own built-in 
type checker, it will probably come with a runtime switch to enable or 
disable such type checking. But that's possible regardless of whether we 
use decorators, annotations, or both.


> Also, Types and Programming Languages by Dr. Pierce has been mentioned 
> at least twice as well; would it be useful to ask him to join in the 
> discussion?

Does he know anything about Python? Will he care? There are hundreds of 
programming languages, unless he has a particular interest in Python I 
can't see why he would care about this discussion. But if you are a 
colleague or friend of his, by all means invite him to join up, this is 
a public forum.



-- 
Steven


More information about the Python-ideas mailing list