3 ms·
If it were a compiled language where I had to wait to run it, I could see that being a big help, but in python I just constantly run the parts of the code I'm e
by dec0dedab0de 6y ago
If it were a compiled language where I had to wait to run it, I could see that being a big help, but in python I just constantly run the parts of the code I'm editing. If there are any typing issues I catch them almost immediately.
To be fair, I do set return types in my docstrings, and let the IDE help there.
My gripe really stems from using a libraries that check for a type and raise an exception if it doesn't match. Instead of just trying to actually use it and see if that fails. for example If you require a dict because you're going to expect certain keys, but I have a class that has __get_item__ for those keys, it should work fine. If you check the type then you'll fail before you get that far.
- kllrnohj 6y agoImagine you need to change the type that's passed to a function. You attempt to update all places, but you miss a path that's in an edge case. Static typing would have caught that for you. By waiting until runtime, you've now just potentially shipped a regression. As a code base gets large, it's just not possible/practical to run all possible ways the code you're editing can be reached. Unless you happen to be lucky enough to have 100% unit test coverage, which very few people do, and running that test suite is somehow quick enough to be a practical alternative to static typing.
- dec0dedab0de 6y agoIf I need to change a type thats passed to a function that already exists and is heavily used, I would either cast the old type to the new type so both work, or create a new function. If I really cared about cleaning it up I would add a deprecation warning when its done the old way.
- stevesimmons 6y agoFor doing "duck-typing" formally, you need Interfaces or Protocols, rather than checking from a shortlist of allowed types. Here's a blog post [1] from this discussion on Django 3.2 [2] 10 days ago. [1] https://glyph.twistedmatrix.com/2021/03/interfaces-and-protocols.html https://glyph.twistedmatrix.com/2021/03/interfaces-and-proto... [2] https://news.ycombinator.com/item?id=26710013 https://news.ycombinator.com/item?id=26710013
- stevesimmons 6y agoActually, this precursor blog post focusing on typing.Protocols is even clearer: https://glyph.twistedmatrix.com/2020/07/new-duck.html https://glyph.twistedmatrix.com/2020/07/new-duck.html