3 ms·
Type hints are almost always a crutch or fix for bad design/unclear data flow/`None` abuse. The idealist in me wishes type hints had never been introduced and
by guitarbill 8y ago
Type hints are almost always a crutch or fix for bad design/unclear data flow/`None` abuse.
The idealist in me wishes type hints had never been introduced and we would just write obvious, beautiful code. The pragmatist/realist is happy because type hints are a gradual improvement. The pessimist is saying that the bad devs who introduce awful data flow and abuse None also won't bother using type hints/getting them right, so why bother.
Pick whichever you feel is most appealing.
- nerdponx 8y agoType hints are almost always a crutch or fix for bad design/unclear data flow/`None` abuse. If you've ever worked on a Python code base that you didn't design and write from the ground up, you would know that these are inevitable, and in many cases encouraged by the language. Even if you did design and write it from the ground up, and even if you meticulously document every class and helper method, the reduction in cognitive load is tremendous. And if you are writing a library or API that other people will use, it's an extra layer of documentation convenience. Why force people to peruse your 200 word docstring when they could just see the signature pop up in their IDE? edit - Consider this example: Country = NamedTuple('Country', [ ('iso2', str), ('iso3', str), ('full_name', str), ('short_name', str), ('other_names', Sequence[str]) ] class Person(): __slots__ = ('age', 'name', 'birth_country') age: int name: str birth_country: Country def force_standardize_name(self) -> Tuple[str, str]: """ Attempt to coerce a name into standardized First, Last form """ ... Maybe my design is bad, but just having all that information written right in the source code makes it so much easier for me to work. Not to mention, I can actually run the Mypy static checker on this and verify that my code does what I think it does.
- guitarbill 8y ago> If you've ever worked on a Python code base that you didn't design and write from the ground up, you would know that these are inevitable, and in many cases encouraged by the language. I have. Maybe it is "inevitable", but saying Python encourages this is a stretch. Those things happen in other languages, it's just called `null` abuse instead. > Not to mention, I can actually run the Mypy static checker on this and verify that my code does what I think it does. Until it doesn't, e.g. at runtime. Type hints are a nice tool, but are not a panacea. They are not the same as variable types. This is probably the most dangerous thing about them. If you want that, Python is not the best choice, type hints don't change that.