"Should Python enforce Type-checking in the future?"

Python already enforces type-checking. It is *runtime* time checking.

https://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

As far as *static* type checking goes, the answer is No.

(Obviously all of the following is my opinion, not holy writ.)


(1) Moving mypy into CPython would be bad for both mypy and CPython.

The release schedules and stability guarantees of each are different. It 
would require the influx of a huge amount of complexity into CPython to 
integrate mypy. And to save what? mypy is free, and easy to install if 
you have an internet connection. The benefits of integration are tiny 
compared to the costs.


(2) Requiring static type checking would be bad for alternative 
implementations.

Python is dominated by CPython, and we should be mildly concerned about 
that: languages dominated by a single implementation (like Ruby and 
Perl) are far more likely to lose popularity and fade away than those 
with many implementations (like C). Anything that makes it harder for 
new implementations to get started is a negative.

Right now, we do have PyPy and MicroPython, both reasonably popular, but 
the other "big" implementations Jython and IronPython are stuck in the 
Python 2 world. Making it harder for them to catch up would be a 
negative.

How hard would it be to migrate PyPy to enforced static typing? What 
would that do to the smaller Python implementations? How would it affect 
Cython and Numba and other projects?

MicroPython needs to run on tiny machines with limited memory resources. 
It will probably never support mandatory static type checking. Running 
Python on tiny machines like Raspberry Pi and Arduino is a big part of 
the Python ecosystem, so mandatory static checking would break the 
language and ecosystem into what is effectively two languages: classic 
Python 3 and new "Python 4" with mandatory static checking.


(3) Have we learned nothing from the 2-to-3 transition of the pain and 
extra work and disruption that a huge backward-compatibility breaking 
change to the language makes? Faced with a major migration from 3-to-4 
that would be as big and painful as 2-to-3, many projects and libraries 
would just abandon Python altogether, in favour or rewriting their 
project in a "real" statically typed language with proper stability 
guarantees.

We should not be talking about Python 4 here, we should be thinking 
Python 5000 or even Python 6000.

More seriously, incremental changes to the language is fine. Massive 
breakage all at once, let's not repeat that.


(4) Mandatory static types would be bad for students and beginners, bad 
for scientists, bad for casual users, and bad for system administrators 
who still use Python as a scripting language. It would make the language 
harder to use and harder to learn.

Python is, not unique, but very unusual in the broad range of uses 
people put it to. It is a *teaching language* that can be used for 
professional projects. It is a *glue language* and a *scripting 
language* that is written by people who just want something that works 
and don't want to fight the compiler's type checking errors to run it. 
That's why dynamic type checking was invented in the first place.


(5) Requiring static type checking would be bad for mypy. mypy is 
designed for *gradual typing*, that is, type checking when only parts of 
your code have static type hints. To move to a world where type hints 
are no longer hints but mandatory strictly enforced types would likely 
make a lot of mypy obsolete. It would likely take a lot of work to rip 
out the code that assumes gradual typing and replace it with code that 
strictly enforces types.


(6) Requiring formal static types goes against modern language design 
for applications languages. Python is not a systems language like Rust. 
Modern applications languages tend to converge to the middle of the 
typing landscape:

- languages with static types tend to develop ways to escape the type 
  system and allow some dynamic typing features; e.g. Java has generics
  and reflection.

- languages with dynamic typing tend to develop optional ways to enforce 
  those types statically.

This is, of course, exactly where Python is now. A fundamentally 
dynamically typed language with optional static typing features. And 
that's a good thing.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5GJVGEWWUB6WHY2JGQ547UIIWDZQHYCE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to