4 ms·
There are lots of ways to ensure correctness; tests, types, formal methods (not caring and just having bugs), but probably the biggest eye opener for me was int
by wokwokwok 6y ago
There are lots of ways to ensure correctness; tests, types, formal methods (not caring and just having bugs), but probably the biggest eye opener for me was introducing new programmers to a python code base without type hints.
How do you know what the argument “details” should be?
The function documentation says “takes the query details” ... so people sit down in the repl and just screw around with the functions to try to figure out what they do, or search for examples of how they’re used.
with type hints, this discovery process for other programmers was significantly quicker and on boarding was faster.
I mean, sure, just write better docstrings, but type hints can’t get out of date, because it’s a type checking error.
Maybe a better solution is rust style “compiled doc strings” so the documentation can’t be wrong, or rust style inline unit tests that show examples of how to use functions... but python has neither.
It’s been my experience that type hints for your own code... eh. But if you’re working in a team, they’re valuable.
- IgorPartola 6y agoIt might be in the nature of what I work on. If I don’t know what arguments a method or a function takes I go and read its source. It is usually very quick and obvious to figure out if it’s a simple type: int, dict, list, float, string, etc. If it isn’t, type hints won’t save you: charge_customer(order_details: dict) That fact that it’s a dict is the least of my worries. I need to know what goes into it, for which I need to either read the docs or the code. So you try to organize it more and avoid using something like a dict of order details. Instead you create an order object that has methods like add_item(id, qty). Can you guess what those are or deduce it quickly by looking at docs or the implementation of the method? Most likely. I’m not saying that type hints won’t help. But as far as just type checking at compile time they only really work as a very obtuse tool, and at runtime they are a very feature-barren form of contracts.
- wokwokwok 6y agoSure, but I challenge you to argue they are a worthless tool. You can do it in other ways, sure... but I don’t accept that you can argue type hints have no worth. ... if they are the correct solution for every case? If they are worth the (honestly fairly trivial) effort to write and maintain? Those are fair questions. The question is, if you’re or using type hints what are you using instead? ...I how the answer isn’t “nothing”, because that just means you not using a tool because you don’t like it, not because you have a good reason.
- IgorPartola 6y agoThey are absolutely not worthless. I just don’t understand the hype around them as being the must have feature.
- jeremysalwen 6y agoThe problem is that the source code is often just a wrapper for another function... which you then have to read the source code for, etc, etc. I can't personally guess the type of the `id` argument to the hypothetical `add_item(id, qty)` function, but I would guess that the source code for that function would look like def add_item(self, id, qty): self._items.add(id, qty) I would also note that python type hints can be charge_customer(order_details: Dict[Product, int]) which is significantly more helpful than just knowing it's a dict.
- IgorPartola 6y agoWhy would you add the unnecessary level of indirection with the _add_item() method? And if your application uses standard IDs throughout then yes you can guess it :)
- kortex 6y agoThis happens all the time. Usually at api intersection boundaries. I do this a lot interfacing camera drivers in some flavor of C with C++ run times, so most gets passed through, but char* goes to string, pointers go to shared_ptr, etc. I also use it a lot at boundaries where the modality changes, eg going from local data structures to RPC, where the data shape is the same, but there is io and fault tolerance to deal with.
- david422 6y agoRight, but you and every other person has to go read the source, and the chain of that source, forever. When really that information could just be in a type hint and save everybody the time.
- BerislavLopac 6y agoPython has doctests.