I am starting to appreciate how Python type serve as extra documentation, as well as how they are bing hacked to make stuff like data classes.
Nevertheless, I feel they are sometimes overused, and maybe we need to think about guidelines, so we can have a better balance between clarity and verbosity.
We learned from experience that clear code doesn't need comments. Functions and variables with good naming do not require comments. For example, is the following comment useful?
# Adding a and b and putting results in c
c = a + b
Not saying that comments aren't needed at all. In fact they are essential for the subtle details not that clear. Or as John Ousterhout put it, “Comments augment the code by providing information at a different level of detail.”
Now with type hints, I want to argue the same, obvious stuff don't need to be annotated. Here are some examples.
- args and *kwargs: We all know that these are lists and dictionaries respectively.However, I've seen code where they are annotated still.
- dictionaries: Like 99% of the time, the dictionary keys are strings, yet I've seen them being annotated with Dict[str, float] or such. Sure, maybe you are dealing with a case where the dict keys are something else, but then I'd argue that the reader needs to know more about that dict then and what it carries then its types.
- When a function doesn't return anything, should we still annotate it with None, or we are kinda stating the obvious here.
- When the variable name is obvious. Do I still need to tell the type if the variable is called timestamp, days_since_creation, is_valid, num_item_sold, currency, or amount_paid?
- All data scientists use df to refer to DataFrame, the name df should be enough to infer the type and a type hint here will be just noise?
The point is, I want to know what do people smarter than me think? Maybe, I am wrong about it, and type hints still offer some value that I miss here.
Finally, one might argue that obvious types hints need to be stated still to be enforced by tools like mypy or so. That's a different argument, and personally, I feel like type checking is a way to hack a dynamic language like Python to become Java. So, I'm only interested here in the documentation value of type hints.
I’ll figure out the bug eventually either way, but having the compiler give me the clearer warning earlier in the development process (sometimes even from pure static analysis) is a significantly more pleasurable development experience and a potential time saver
And 50X that, especially the time saved, if I’m integrating with code you didn’t author.