![](https://secure.gravatar.com/avatar/d94a17e5d9fbb5c20ff568b9d3a21f48.jpg?s=120&d=mm&r=g)
Hi, (NB: I'm just a lurker here, posting for almost the first time. I may say silly things.) Le 09/12/2021 à 21:32, deavid a écrit :
Hi, I would like to hear the opinion of Python's community on enforcing types in the future for the language. I've been using Python as my main language for everything for around 10 years, until I started moving to Rust 2 years ago; one of the main factors was types.
Just before moving to Rust I started to use mypy heavily, which I liked a lot and uncovered tons of potential problems. Now (2 years later), it seems the situation hasn't changed much; I might be wrong, so let me know what improvements you think landed in this area in the last 2-3 years.
I don't follow typing closely at all, but I do nevertheless see quite a number of typing PEPs getting accepted. Here are a few recent ones gathered just by reading the index in PEP 0: https://www.python.org/dev/peps/pep-0589/ https://www.python.org/dev/peps/pep-0586/ https://www.python.org/dev/peps/pep-0585/ https://www.python.org/dev/peps/pep-0591/ https://www.python.org/dev/peps/pep-0593/ https://www.python.org/dev/peps/pep-0604/ https://www.python.org/dev/peps/pep-0612/ https://www.python.org/dev/peps/pep-0613/ https://www.python.org/dev/peps/pep-0647/
I feel it's possible this topic might cause a lot of passionate answers, but I just want to hear honest opinions on this.
I firmly believe that Python's future would be better if types were enforced by default at "compile time" (whatever this means in Python), with an option/flag to disable this, and integrate MyPy or similar into the interpreter. I'm fully aware that a transition like this would be very hard and long, but I don't think it's impossible.
Here's a list of my reasons to think that Python is better if it was typed:
1) On really big codebases and complex projects, it's very easy to lose track of what things do. Types help detecting bugs early. (Ask anyone that has used Rust + Clippy, the amount of errors that are catched is amazing, programs tend to work on the first try) 2) Libraries are currently the top bottleneck for any team to start using MyPy/Pytype. Making types mandatory would ensure all libraries have type support. (If anyone has any other proposal to enforce this, I would like to hear) 3) IDE integration is way simpler and better with types. 4) The interpreter could take further optimizations if it can prove that a function or piece of code is guaranteed to have a limited set of types. This could be used by libraries to have great speed ups that currently are not possible. 5) Static analysis tools could also benefit from types to gain more insight on what the code is trying to do.
These are benefits of types in general, but I don't see clearly how having checks built as part of the standard CPython interpreter will increase them. Performance perhaps, but the projects using annotations to eliminate runtime overhead are in experimental stage. Asking a genuine question here: is it actually possible to generate optimized code that supports all of Python? What about things like int subclasses? MyPyC doesn't support them as far as I can see, and I don't see how it could when trying to generate optimized code. It might only optimize int operations when it can prove that all callers don't pass int subclasses, but this means recompiling when a new caller does pass a subclass -- which turns Python into far more of a compiled language. Or am I missing something? On the other hand, with the specializing adaptive that is the result of the Faster CPython project, it might become eventually possible to specialize based on types, and have a cheaper code path when the annotation is int that will still fall back to a slower path when an int subclass instance is passed. Also, you seem to imply that having typing more tied to the core language will bring it more popularity. How will this happen exactly? People don't like it when you break their code. If Python 4 causes trouble for users who don't need/want typing, these users will not suddenly start using typing, but simply stay with Python 3. You'd apparently want to require libraries to be annotated for them to support Python 4, which means pain that the ecosystem simply cannot afford, I think. -- And if the annotations remain optional, then I guess I don't really understand how it's going to push people to add them, especially on large libraries, where this is a significant endeavor. All the best, Jean