177 points · by lelf · 6y ago · 219 comments
Python is just an incredible language to learn programming in and an extremely productive language for building small-to-medium sized apps. Type hints + making type checking part of the build stage helps, a lot in my opinion. With these + tests in place I almost always experience that I can refactor safely.
I would use a statically typed implementation of Python in a heartbeat, I really hope it gets made.
I'm baffled at all the python comparisons. Yes, it as significant white space, but that felt like it. It felt more like Ada or some other language than python.
The one thing I did appreciate was `(;)` for turning statements into expressions. I've grown to love having complex expressions in Rust and miss them in other contexts. However, Python's scoping reduces the need for them.
It is so joyful to install a library, import it, and then to explore the API through my turbo-charged IDE thanks to the type-hints provided by the type files.
https://www.youtube.com/watch?v=Z8vsTxzmorE
https://openteams.com/initiatives/2
> "We will create an embedded domain specific language (DSL) using the Python language itself along with the typing module and specialized objects as necessary to allow nearly all the existing extensions to be written using this DSL. We will then port several key libraries such as NumPy, SciPy, and Pandas to use this DSL. We will also provide a runtime so these extensions can work on PyPy, C-Python, and RustPython."
I'm pretty sure we'll see a strict, typed subset of python that is jitted or compiled at some point in the next few years.
Edit: Oh also mypyc https://github.com/python/mypy/tree/master/mypyc
But I just saw a very similar project by the name "HPy" on the PyPy blog:
https://morepypy.blogspot.com/2019/12/hpy-kick-off-sprint-re...
The idea of HPy was born during EuroPython 2019 in Basel, where there was an informal meeting which included core developers of PyPy, CPython (Victor Stinner and Mark Shannon) and Cython (Stefan Behnel). The ideas were later also discussed with Tim Felgentreff of GraalPython, to make sure they would also be applicable to this very different implementation, Windel Bouwman of RustPython is following the project as well.
And they even say NumPy is a major use case:
Eventually, we will also want to try to port parts of numpy to HPy to finally solve the long-standing problem of sub-optimal numpy performance in PyPy.
Anyway there's nothing wrong with competing projects. Progress gets made faster that way. But I guess it's weird that the video and the blog post have very similar dates but don't mention each other. The problem they're solving seems essentially identical, and they even mention all the same Python interpreter implementations.
We've added type hints in most places. And it makes it a lot easier reading other people code and seeying their intentions.
Code is read a lot more often than that its written. And just being more explicit in what it does makes it better.
Also type hints allow for certain static verification, making the code quality better.
The big thing i miss is: Python is not a pure typed language.
Programming in Kotlin or Golang feels so much more "engineering". I feel a lot saver. When it compiles i'm 100% sure which types are coming into my methods...
With python it can always break in production.
Choosing a type save language is the way togo now. Languages like python and kotlin are just as expressive as python, but they are just engineered a lot better imho.
That's one of the problems with the idea of such an optional not-integrated type checker, compared to a type system.
Untyped values can freely change its types during runtime, typed variables not. Its fundamental safety mypy is violating here. types are giving a strong guarantee here. Only untyped const variables may be inferred.
Furthermore 3 more fundamental type system features are missing:
1) Typed variables can be unboxed if it makes sense for the compiler. at compile-time, not run-time.
2) some loop counters can be checked against array indices at compile-time, run-time oob checks can be omitted then.
3) the ffi needs types to be integrated. you can even layout structs in native classes as expected in C, completely unboxed. which is about 4x smaller and faster to use. and much easier to use.
type checking only is only about a third of a real type system. I've implemented a proper type system in cperl, and worked happily also with perl6 and other integrated gradual type systems, mypy and similar systems for other languages seem like a joke to me. a hint is something different than a guarantee.
Optional type declarations for function parameters, checked and used by the compiler, would make sense. PyPy could use them to optimize basic numeric types. Within a function, you can usually infer types. From an optimization perspective, the only types worth distinguishing are probably int32, int64, slotted classes without inheritance, and everything else. "Everything else" needs the current dynamic dispatch mechanism; the other cases can have hard machine code generated.
But this optional stuff? That looks like a solution to a political problem, probably avoiding it being totally ignored. The Python designers used up their goodwill budget in the 2 to 3 fiasco.
Furthermore, the duck typing paradigm further warrants Python's position here. In the above example, the spell checker code can work with a variety of string types, or even lists, or even perhaps the Tensor-like things found in Tensorflow and Pytorch. Without duck typing, the author has to use generics and the user has to play a type game that can become obscene-- see Java.
So that's a dominant mindset that sets the context here; things no doubt look strange from a different perspective.
I agree it's odd that the interpreter doesn't enforce static type declarations by default. I've seen now two professional code bases where the authors used type hints everywhere and I found the code actually disobeyed the hints. (I guess nobody ran mypy regularly).
That's probably what the Python committee anticipated: a few people would use this feature correctly, and hopefully build polished linting solutions as well. So they gave them an affordance for type hints. But most people would not want this at all, so it's entirely opt-in until it's something polished.
Going to have to disagree with you here. Have you ever used Typescript? You can have duck typing and a proper static type system at the same time without devolving into Java. Furthermore, you can write Typescript without using types at all! Simply have everything default to `any`, and it reduces back down to Javascript.
Python unfortunately decided to go in a direction that is the worst of both worlds. You can add type hints, but they do nothing without a 3rd party tool (?!). This means I can enter a code base, see type hints, and still have no guarantee that the types are what they claim to be. Instead, we have to rely on company-specific build processes to give us that static guarantee. And if you're not lucky enough to work in a codebase that runs mypy before merging, you might as well throw those type hints out the window.
I don’t believe you can really have duck typing and static at the same time unless you throw out the static checks and mark things as “Any” (or the equivalent). The beauty of Pythonic duck typing is that interfaces need not have any type declaration and furthermore the user can use dicts with complex structure (and declaring that structure with type annotations wouldn’t add any value over examples and/or unit tests).
What makes Scala and modern C++ great is they offer nice interfaces for expedient use of the static type system. But there’s still plenty of room for the Pythonic approach of no static types at all.
Just add word: str to all the functions and it will make it impossible to accidentally call any function with an invalid type. Especially the known(words) function is easy to misuse by providing a single string instead of a Iterable[str]. Iterable is a generic btw and will support both sets, lists and other similar types without any obscene type game. mypy also supports static duck typing with Protocols.
> Plus the strangest feature of all - a mechanism for turning off the optional type checking so old code will still compile, even if it won't work right.
Most languages have this, in some form or another. There are ways to get around type checking when the compiler misunderstands something. How is this unique to python?
Some at my company are wanting the same so I’ve started developing https://github.com/thundergolfer/Bazel-mypy-integration.
I hadn’t used MyPy much before this work but I’m already quite happy with it.
In [1]: a: int
In [2]: b = 5
In [3]: c: int = 8
In [4]: __annotations__
Out[4]: {'a': int, 'c': int}
In [5]: c = 'foo'
In [6]: __annotations__
Out[6]: {'a': int, 'c': int}
Tinkering with __annotations__ is also needed for hy (python lisp(-like?) language, http://hylang.org/)
Did anyone try this with things that interface with Fortran or C?
That's why I think the type hinting thing is always a bandaid.
Honestly, how often have you had to write a function that strictly takes a char and not string? In a decade of programming professionally, I can probably count 3.
That's not a typechecking issue so much as a type structure issue; “there's no Char type” isn't a problem of (any of the) Python typecheckers, Python simply has never had a char type; strings are iterables whose members are strings which, while drawn from a specific subset, do not have a distinct type.
This may be a type system problem, but it's not a type hinting problem.,
It's actually a fairly easy to fix issue for the most part, but I don't have time to push for inclusion/adoption.
I say this as someone who grew up in the 90s with my father's Pascal textbook and a deep love for Turbo Pascal.
I.e. is there some way to transpile code (much alike TypeScript to JavaScript), so that the code can be installed by lower versions of Python than 3.6?
Contrast this to my experience with C# in visual studio (not visual studio code, which is inferior). The state of flow I can get into when performing large scale refactoring, with immediate, accurate feedback in the form of a clickable, line by line list of what's broken is unmatched. I would love such a pleasant experience in a python IDE but I'm not sure it's possible because of the nature of duck typing. It's like a real time, automatically generated checklist of exactly where to propagate changes, takes a massive load off my working memory and provides an uninterrupted flow of dopamine. A true state of zen if I've ever experienced one.
Back when I wrote C#, static typing was indeed helpful for making large-scale changes and providing an automated check that this wasn't destroying everything. But then, it's only in languages like C# that I have to make these kinds of large-scale changes. There's no macros or syntactic abstraction. When you want to construct the same category of operation in different contexts, and functions and classes don't operate on the right axis, you're SOL. You just end up repeating yourself.
When I write Lisp, I never have to make changes that cover more than their one area of responsibility. At worst, I'll rename a function, and a simple textual find-and-replace is more than sufficient -- better than most refactoring browsers, even, since it will hit my comments and documentation. Do fancy refactoring tools update your README and docs/ folder yet?
(In one case, I knew an experienced C# programmer to build and compile an expression at run-time using LambdaExpression [1]. It takes about 10 times as many lines to achieve the same thing, and you have to write in a style that looks nothing at all like a normal function, so in practice nobody ever does this. In contrast, the way to accomplish this in Lisp takes one character, and the code looks identical to a normal function, so it's not unheard of.)
I feel that Python, in many ways, combines the worst aspects of both worlds. It's not strict enough to be a good static language, and not powerful enough to be a good dynamic language. Sadly, the most popular dynamic languages today are Python and JS and PHP, so a lot of failures and limitations of these languages get blamed on dynamic languages in general.
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.linq.expr...
Yes, but Python was never meant to be a static language. The static type annotation feature was borrowed from static languages to reduce bugs, not to make Python a static language.
> Python ... not powerful enough to be a good dynamic language.
I strongly disagree with this. Python is a very powerful dynamic language. It lets you code with any paradigm that suits the problem you’re trying to solve. Functional, OOP, procedural, or whatever. CPython has hidden features that allow for low level customization of how it works (when it is appropriate).
> Sadly, the most popular dynamic languages today are Python and JS and PHP
There is a big difference between Python and those other languages. JavaScript and PHP became popular because they had to be used to write frontend code and backend code respectively for websites on affordable web hosts. Python became popular because programmers chose to use it; because it’s really good.
Perhaps you think Lisp is better; that’s fine, but notion that Python is not good or not powerful is just wrong.
I didn't figure out why things are like this. I'm guessing something about Lisp makes me write software in a way that defeats the need for automated refactoring. It's not only that they're not possible to implement reliably[0], I don't find myself in any need of them.
Also, spot on with OAOO. I have cases where equivalent Java code could occasionally benefit from automatic refactoring of couple dozen functions and classes, because they're all hand-written. My Lisp code is usually one macro invocation that generates all these functions/classes, so "automated refactoring" involves changing the macro definition (and rerunning the top-level form using it, in the live image I'm working in).
--
[0] - Good luck automatically refactoring calls to slot-value that use slot names generated at runtime, or any kind of non-trivial macro. That said, I sometimes wish this wasn't the case, because I could use better autocomplete, or more reliable "who references this" queries.
The clencher was after rewriting a large app in F# and having two working versions for a while, I decided to move to async calls. In F# I was able to change a few core functions and then follow the red squiggly lines, wrapping logic into async and asyncSeq monads until everything compiled again. And after a day of doing so, everything worked the first time. With Clojure I couldn't even find the courage to begin something like that. It would have to be a complete rewrite. I never touched Clojure again. Maybe lisp is different.
For others - http://c2.com/xp/OnceAndOnlyOnce.html
They don’t need to have the type declaration as most things can be inferred.
So you get the feelings of writing Python with the full suite of benefits of a strongly typed functional language when using say F#
Yes actually. Resharper plus Swagger handle 95% of that for me automatically.
And I absolutely do not agree with your point about syntactic abstraction. Interfaces, dependency injection, generics, very light use of inheritance/abstract base classes, and extension methods generally provide all the abstraction I need to avoid repeating myself.
Could you elaborate? I guess you're comparing it with Lisp -- what makes it significantly more powerful than Python? (I read your paragraph about Lisp and I don't see why it couldn't just as easily apply to Python, apart from `lambda` being 6 characters instead of 1.)
And of course macros are useful for making DSLs.
Rust's macro system(s) are pretty powerful (ergonomic, hygenic - typesafe, simple, as expressive and powerful as you wish via procedural macros that can do almost everything with the AST).
Safely and cleanly abstracting any kind of program even with DSLs is rarely easy because of the need to pass the right context, to find the right interfaces, to manage data/state/context/systems dependencies, and something like Scala3's implicits might help with it. (Though maybe simply more vigorous refactoring might also work to keep data-passing to a minimum. But then I fear that turns into an over-abstraction fest, and we get back Java's FactoryFactory-like monsters.)
Given the performance it allows for run-time generated code, nothing in production really beats the DLR. It is a shame Microsoft nerfed it in UWP because they ripped out the jit in favor of ahead of time compilation.
Take SBCL - an open source Common Lisp implementation that compiles everything by default. That includes any run-time generated code. The code is simply AOT-compiled at runtime, at the point of generation.
What's great with Python type annotation is that you can give it as much as you want, and it'll do the best it can with that. If you don't want to give it any type, then don't. If you want to give type for one function, then only do for that. You get to choose how deep down the well you wan to go.
This ideal "middle state" I speak of is a language that is both as easy as a dynamic language but as safe as a static language. Static languages are making it easier to write and dynamic languages are making it safer.
And the cost of this flexibility seems to be quite high. The overall quality of type checking that mypy gives you seems to be pretty abysmal, and it requires quite a bit more handholding and awkward workarounds to deal with the proliferation of any's and uninferable types than a language that just starts with types in the first place.
So already, with zero annotations, you already get value out, let alone once you type a few tricky variables that are harder for Python to track.
For fearless refactoring, sure, you need quite high type-coverage to not get a false sense of security, but still, the more you have the safer it is. For just finding bugs and code navigation, any addition of types is going to be a win.
That's also what's Not Great about this whole 'annotation' or 'gradual typing' approach. Having only some arbitrary part of the program be statically typed introduces a huge amount of 'interaction' or 'blame' points between the static and the dynamic portion of your program, that significantly impact the usual benefits of static typing. Dynamic types should be used only when strictly necessary, everything else should be made static as soon as feasible.
Languages like python with a typechecker are much closer to rust (safe, but with the ability to have unsafe areas) than true statically typed langs, because static languages don't give easy escape hatches (you end up having to cast everything, no static Lang I'm aware of ships with an Any type like python's).
The cool thing about python is that the stdlib is readily memorizable. It's a little idiomatic, but it's relatively small. The onus is on the IDE--if you want one--to be smart. Type hinting probably doesn't catch many bugs, especially with metaprogramming, but it might add some value to large codebases or team development.
That's a condition for all TDD.
> I know there's testing frameworks, although it seems more of an eclectic mess than Java
If you look at the test suites for popular libraries like numpy, django, airflow it's mostly `pytest`, `unittest` (part of the standard library), and `nose`.
> where testing and mocking is just a little harder to do...
Mocking is actually pretty easy to do in python using pytest.monkeypatch or unittest.mock. Compared to mocking in a strongly typed language like C++ (and I assume Java) if an object you're mocking implements a particular interface, you would only have to mock out the parts that get exercied by the codepath in the tests you care about.
The other popular testing library is pytest. So that's more choices than in Java but fewer than in C#.
A lot of the smartest people are super productive and super considerate of all the possible codepaths when making changes. Bugs still occur due to statically discoverable coding errors.
Just how much harder it is to navigate a new codebase is staggering.
That's because that however good the IDEs are, their language is full of ceremony and stiff-OO abstractions, and Python can achieve in 10 lines what takes Java 100.
Plus it interfaces much better with C/C++/Fortran/etc code, which serious data science libraries are written in.
This makes it much better fit for science than Java. Plus a lot if not most of science code is short scripts and one-off. So the benefits of types for larger projects etc don't come in at all.
Unlike enterprise users, scientists wanting to write a program don't have no time for 100 lines of ceremonial crap to make it look "enterprisy" or have much tolerance for a language with much less expressive power.
Learning a dynamically typed language like Python is moderately easier than learning a statically typed language, especially old-fashioned languages like Java without type inference. OTOH using a statically typed language, once you've learned how to handle it, makes for a more powerful software engineering experience, but you'll have to climb the hill of typing first.
I can appreciate making an investment in tools that make me more productive in the long run, but at some point it has to start paying off. Programming languages have finite useful lifetimes. Common Lisp took me years to learn well, too, but it started paying big dividends after only a couple weeks.
Personally, I would not attempt large refactorings without a decent test suite. I consider enabling fast and safe refactoring to be the main advantage of test suites!
Writing tests is also an absolute must in many organizations.
My advice to you is to look for work in organizations that are not garbage. We're out here!
In terms of dynamic languages, the Clojure ecosystem has a lot of techniques that help with dynamic language codebase scalability. Very few are exhibited in Python codebases I see, I imagine for various reasons.
The most important is short, single purpose functions arranged in layers- essential for working at the REPL, where you may want to inspect or interject at any point in the data flow.
Related is a clear segregation between code and data, and explicit data flow/state control. I see this violated all the time in Python classes, which are very clever but should almost never be used.
Clojure Spec is also a revelation, much more expressive than a static type system and also (IMO) much more comprehensible. Nothing equivalent in the Python world.
Python works hard to be readable in the small, but it is a struggle in the large, both because the language does not help you and because the ecosystem is still pretty immature.
However I don't think the Python ecosystem is immature, for me it is the contrary. This project started with Python since I didn't find a good way to reduce the Clojure startup time, and believe me I tried everything I found (from GraalVM to ClojureScript). And there is gaps everywhere in Clojure ecosystem when you want to create command line tools, while Python has literally everything I needed.
Could you expand on this one?
Although Python does a lovely job in defining type-like protocols that classes can participate in- e.g. all the special dunder methods- most Python code in the wild that I see is not beautiful-in-the-large, well-developed, reusable, leverageable abstractions. It's just business logic, conditionals, utility code, scripts-turned-into-apps, etc. Plenty of reusable functions, very few reusable types.
Putting business logic mush into classes with some state and calling it a type is a straight line to unmaintainability. It becomes very difficult to extend such a system with new "types" or to modify behavior of existing types, because there is a lack of clarity about the semantics of the existing types.
Rather than trying to define types with state, much better to treat state as data- the difference is that the term state implies some special smart type-like semantics, while data is just dumb keys and values.
The comparison I drew was with Clojure, which doesn't make it so easy to make classes that hide state or create fake types. Instead it has graduated options for lightly packaging data elements together- start with maps/dicts and move onto records- and optimizes for just chaining functions that operate on those data blobs. "Simple", as Rich Hickey famously says. There is no spaghetti-inducing tension between keeping logic "encapsulated" vs coding it "in the open."
For regular line of business apps, the latter is so much better when it comes to maintainability, but the presence of classes provides a constant temptation to find reusability/encapsulation/abstraction where it doesn't actually exist. In Python, IMO, classes should only rarely be used.
Hope that helps?
It’s not quite the burden hn-ers make it sound.
What...? Everything you describe here is the symptom of a bad codebase, not the symptoms of a dynamic language...
> I end up having to rerun the code repeatedly to find and squash bugs, and that says nothing of the code paths I don't end up taking
Everything in their post describes real world code and all projects beyond toy-size.
Type hints and doctests.
If that doesn't get you to pretty big static typing wins, then you are probably just dealing with a toxic codebase or something.
Can you suggest some modern, best practice C# projects for me to see how the language should be used?
I'm curious about this as well.
I don't think many people disagree that strong typing is a requirement for huge projects.
In C++ you think hard about your problem, create strong abstractions that translate into fast execution. That's a highly satisfying engineering job.
In Python you do happy exploratory programming. Changing data structures on the fly, hacking your way through unexpected problem by doing crass things like adding members to a class at runtime.
You can do exploratory programming in C++ but that requires a lot of additional work to experience a fraction of that freedom. And you can do solid engineering in python but that requires as well a lot of additional work.
Different tools for different things. Python is the off-road bike you take for a bit of fun in the forest. C++ is the family car you take to do the week's grocery.
C++ is an awful language that requires too much thinking. Make a mistake and you'll pay for it with hidden memory leaks and segfaults. It offers no help. No other mainstream language is quite so unforgiving. Rust, Go, and Swift will hopefully relegate it to the dustbin. (Of the three, only Rust is suitably non-GC'd for bare metal requirements.)
> C++ is the family car you take to do the week's grocery.
C++ is the Soyuz space capsule. You'd better pack a gun, because there might be bears where you land.
Try using a compiler that wasn't made in the last millennium.
> Type hinting feels like a bandaid for a fundamental limitation of dynamic languages. I've just gotten back to a complex, experimental codebase after only a couple months of absence, and am refactoring it to accommodate for the implementation of a number of previously unplanned features. Even with type hinting and heuristic linting it's such a huge pain! After making a large number of changes I end up having to rerun the code repeatedly to find and squash bugs, and that says nothing of the code paths I don't end up taking. Is there a better way to utilize the convenience of python for experimental code without running into the scalability issues of large python codebases?
What's your unit test coverage like? I'm not asking for coverage percentages (those are effectively useless) I'm asking: have you built out test coverage to the point that you trust your test suite?
C# is probably the best mainstream example of a strongly-typed, statically-typed language[1]. People coming from strong, statically-typed languages tend to rest heavily on the type system. So when they come to a dynamically-typed language, they feel like a vital tool for preventing bugs and structuring code has been taken away from them.
In his essay, Yes, We Have Noticed the Skulls[2], Scott Alexander notes that outsiders often look at a group and see a pile of "skulls" in the group's wake--problems the group has had in the past--without realizing that those problems are just as obvious and concerning to the insiders of the group, and that they are already addressed.
The Python community is no exception: we know that a lack of static types makes our code prone to bugs. And the solution the Python community has come up with is automated testing. It's no mistake that test coverage, test-driven development, behavior-driven development, etc., were popularized in communities built around dynamic languages, and there's extensive, effective tooling for these in Python.
And here's the thing: automated testing gets you more than static typing does. Haskell programmers in particular like to make the claim that "if it compiles, it's correct", but even with Haskell's type system which is stronger than C#'s, that's just not true. Types aren't a silver bullet: there's no shortcut to having to run your code to test, and if you have to run your code to test it, then it makes sense to automate the running of your code to test it.
And unit tests, the static type system gets in your way. Yes, you absolutely can (and should!) write unit tests in C#. But compilation slows down your unit tests. Having a static type system makes it harder to tell where you should write a test and where you should rely on the type system. You might want to pass a null value to a function in a test because the parameter doesn't matter for the test, but if that parameter can't be null in production, you don't want to have that parameter be nullable, because you want your type system to catch that, meaning you have to hydrate a potentially complex object just to satisfy the type system. Mocking is much more simple in dynamic languages--in C# you end up creating a lot of interfaces which are only implemented once, just so you can mock them. The list goes on.
The result is a sort of Pareto 80/20[3] situation: for equivalent C#/Python codebases with high reliability, C#'s type system gets you 80% of the reliability with 20% of the work, but unit testing to cover the last 20% ends up filling up the other 80%.
There is probably an upper bound to this. If you really need 100% reliability, it will probably take an extra 200% effort in C#, but it might not be possible with current tooling in Python. But very few projects fall into this category.
My impression of type hinting in Python is that the endgame for type hinting actually has little to do with finding bugs: it's more about optimization. The fact that type hints can be used for code verification is just a happy side effect.
As a final note, I'll add that assertions are an extremely useful tool which I feel are underused in both the C# and Python communities. In C#, assertions can fill out a lot of the gaps where the type system can't catch errors. In Python, assertions can be used to verify constraints without the boilerplate of a unit test (the tradeoff being that an assertion doesn't inherently run when you run your test suite). In both languages, assertions increase the value of your unit tests (because they test the constraints whenever the code is run in the unit test, even if the constraint isn't what the unit test is intended to test). And in both languages, assertions make debugging easier because they move the error detection closer to where the bug originates.
[1] There are better examples of strongly-typed, statically-typed languages, but they aren't mainstream. I'm not criticizing anyone's language, so please don't bite my head off about this.
[2] https://slatestarcodex.com/2017/04/07/yes-we-have-noticed-th...
To me it feels like python is running as far away from simplicity as fast as possible.
I'm not a PL theory guy (so correct me if I'm theoretically wrong), but it turns out you can usually infer types for everything as long as methods and functions are annotated. It only breaks down in the case of complicated generics or heterogeneous containers, and in those cases often only when there are chained method calls that prevent inference of the intermediate calls.
Typing at the function level is extremely useful for all languages, both statically and dynamically typed. Python, TypeScript, Rust, Java, etc. all benefit.
A broad class of errors are caused by passing the wrong types into functions. In scripting languages, these creep in due to mistakes or refactors. Types on functions can prevent this. It also helps you avoid writing tests or precondition checks for the wrong type of input.
Speaking of refactors, having type information on function calls lets you do codebase-wide function signature changing, method renaming, etc. with the assurance you won't break your code or miss any calls. It's a killer feature.
Even if you gained nothing from the practice, you always want to know what you're required to pass a function.
def resize_image(image, width, height, strategy):
pass
What do any of those parameters mean? Do you want to rely on documentation? That can get out of sync, especially during refactors or in multi-engineer projects.Types on functions are the way to go. Once you try it, you'll never look back.
In the field, Python stays mostly simple. All those stuff are optional, and they are used in this spirit.
In fact, most Python code I see out there seldom even use lambdas, comprehension lists or yield.
You gotta remember a huge part of the Python user base are not programmers, and they don't have the time to figure those things out.
That's one of the reason of Python success: it scales up, and it scales down.
> To me it feels like python is running as far away from simplicity as fast as possible.
Despite its reputation, Python hasn't been a simple language for a long time.
IMO this is worth it, since the goal is less about eliminating runtime errors or other errors, and more about encouraging documentation in code rather than in comments/docstrings. This makes reading code simpler, not less so.
[1] https://mail.python.org/pipermail/python-dev/2015-April/1392...
The fact that you like Python for being "runnable pseudocode" suggests to me that you only use it for very small scripts, in which case it is true you don't really need static types or type hints. But many people use Python for much larger programs and in that case it is really indispensable.
Even just a smattering of type hints helps dramatically, as a few well placed hints on the APIs I know the least, gives me a huge boost.