So going forward my personal projects continue to be ducktyped, and if I care about types, I use rust or go.
So going forward my personal projects continue to be ducktyped, and if I care about types, I use rust or go.
I have ported a codebase from Python 2 to 3 by annotating it with comment-syntax annotations and it helps a ton to keep your strings and bytes separate. Basically, you annotate with the binary_type, text_type types the six package and typecheck your code on both Python 2 and Python 3.
Basically, the steps involved are: 1. set up static analysis (linting and typechecking) for python 2 and python 3 2. get code passing static analysis on Py2 and Py3 3. get code passing tests on Py2 and Py3 4. get code running in production on Py3 5. stop checking and testing on Py2
It's not trivial and it requires some strong Python experience to write code that correctly "straddles" Py2 and Py3. But the advantage of the static checkers is you can slowly dial up their strictness and once they are dialed in, you minimize new regressions (new untyped code, new code that fails typechecking).
Really it needed a huge amount of reorg and refactoring, which a test suite and pyflakes handled quite well.
Type-checking in general is good. However, Python's type-checking mechanisms are bad. The syntax for adding types is very obviously bolted on, they miss basic type errors all the time, and third-party libraries often miss type annotations anyway.
Python is fundamentally a duck-typed language, and that's how you should use it. If type-checking is important to you, use a language that is designed for it.
If there is anything complicated, you can either make a quick generic type (TypeVar) or even just throw in an Any type! I had to do that recently with a function that handled different ROS services, which apparently do not share any base class.
Static typing makes Python tolerable.
Perhaps you haven't used static typing so aren't familiar with the kinds of bugs it can prevent?
I’m talking about realistic errors, not theoretical ones that would crash at first run. You have one test, right? Run the code once before deploying? I thought so. In which case, pyflakes and a few tests find a great majority (not all) of errors devs actually have.
No they don't. First of all people are really bad at writing tests. Secondly, writing tests sucks; I would much rather write static type annotations than tedious type checking tests: you know when to stop and you get the other benefits of static types like making the code easier to read and navigate.
Every piece of code I've added static typing to has revealed bugs. One we later hit in production (because the author was stupidly against static typing so I gave up trying to help him).
That bug was simply calling a method that didn't even exist. It was in a code path that wasn't tested, despite having some tests.
But I can see you're from the school of "I don't make mistakes" and convincing you otherwise is about as effective as trying to persuade Christians there isn't a god.
https://news.ycombinator.com/item?id=39161025
I.e. there are bugs, there are always bugs. But are they important bugs? Is the question. When faced with a house of cards, do you fix the nails, or start with a new foundation?
Also you don’t write tests just for types. You write tests to validate functionality as you would anyway. And they will find type errors on failure.
Black/white adolescent appeals to the one-true-way are not compelling and why you did not convince, then or now. Typing is simply another useful tool in the toolbox, and I never said it should be avoided—my statements were qualified.
You're looking at a bridge made of wet cardboard and saying you shouldn't use FEA when designing bridges because the real problems are the fact that it's made of cardboard and it's wet.
> Also you don’t write tests just for types.
Not just. But you do have to write those tests as well if you don't use static typing. I have seen them written. Static typing reduces the number of tests you need to write. This is very very very well known.
> Black/white adolescent appeals to the one-true-way are not compelling and why you did not convince, then or now.
No, it's because you view static types as an admission that you might make mistakes and you don't want to admit that to yourself. You literally equated making typing mistakes with punching yourself in the face.
Turns out the trivial errors you’ve having trouble with are not a substantial problem with the right tools. As mentioned in my first reply.
I'm moving away from Python for my large projects...