3 ms·
Type checking in Python is a complete waste of time and only leads to a false sense of improvement. People use type annotations for the same reason they wear a
by ltbarcly3 5y ago
Type checking in Python is a complete waste of time and only leads to a false sense of improvement. People use type annotations for the same reason they wear a shirt in the pool, it feels weird to not see those reassuring type names everywhere once you are used to them from some other language. Take out all the non enforced type annotations and spend the time and effort you put into them into writing tests and docstrings instead.
- radarsat1 5y agoOne thing that drives me nuts is that enthusiasts of python type hints claim that it means you will no longer give a function the wrong type of value and thus eliminate a class of errors. This should imply that you no longer need to test for those types of problems. Since writing tests is not free, and often not even trivial, this would be a huge benefit of type hints. But, because type hints are just window dressing in Python, you still have to write those tests. So now when you make a change, you not only have to update your tests but also update your type hints everywhere. As far as I can tell, type hints just double the work for little to no benefit. If they actually removed a need for unit testing, that would be a different story, but then it wouldn't be Python anymore. If I want to use types to enforce things, I'd prefer to use a language designed for that. But I choose Python because it lets me get things done quickly. Despite helping you out in some cases, overall maintaining type hints are a drag on that feeling of getting things done quickly. The last thing I want to do when writing Python is spend an afternoon composing a complicated TypeVar arrangement to express the full range of inputs my function can handle. What this article talks about (Protocol) seems to be at least an improvement in that regard, as you can worry about what your function does instead of what your types do.
- dragonwriter 5y ago> But, because type hints are just window dressing in Python, Type annotations are not window dressing in a Python workflow using typechecking tools over the final code base, unless you explicitly tell the typechecker to ignore a detected error. It's true that a type annotated library can't necessarily rely on downstream clients to have type discipline, and some library packages may not be fully annotated, which may create gaps to address. > If I want to use types to enforce things, I'd prefer to use a language designed for that. Python's type annotations were designed largely to make Python compatible with a separate existing language designed for that purpose; there's a reason mypy’s domain is mypy-lang.org > What this article talks about (Protocol) seems to be at least an improvement in that regard, as you can worry about what your function does instead of what your types do. Protocols are types; specifically, they are structural types.
- DerArzt 5y agoTo add to this, when we are reading/writing python source code there are types involved. The question becomes do we want the types to exist only in our heads or also denoted in the source. To me even though the type annotations don't provide the same kind of guarantees that the type system of say Haskell does, they do increase the readability of the source code. Finally (and this is more of my workflow), I utilize a build tool that has a mypy --strict call that will check my type annotations and alert me when things don't match up.
- b5n 5y agoI know this sounds crazy, but hear me out, I just use statically typed languages when I require static types.
- shankr 5y agoHear me out too - I just take advantage of tools which can help me in reducing bugs especially if I am already comfortable in some language. Type hints do help lot of people.
- dragonwriter 5y ago> Take out all the non enforced type annotations If you are using a static analyzer before deploying, type annotations are just as enforced in Python as in Haskell; that is, they are enforced by a process that happens before and detached from run time.
- JoelMcCracken 5y agoI wear a shirt in the pool because otherwise I get sun poisoning. Sunscreen works but its so easy to miss a part and the consequences are bad enough that i just keep the shirt on -- the skin never gets sun so its the most sensitive part.