Hacker News new | ask | show | jobs
by frumiousirc 38 days ago
Am I alone in thinking type hints are largely a waste of time in Python and can be actively harmful?

I had claude run ty via this new "uv check" on a moderately sized Python package (cli+tui to a multi-transport messaging system) and it found 67 things. Claude checked each finding for bugginess and found 1 is marginally worth fixing. A large fraction of the findings were ty guessing the type and being wrong. Hinting would I guess remove those wrong guesses but hinting is a PITA and greatly increases code size, both of which is bad for human and LLM.

Do people really get benefit from going all-in with type hinting?

4 comments

This feels like rage bait, but yes they do, that’s why it was added to Python after decades of not having it.
I work mostly with Python and I cannot image my life without type hints.

If you ever had to maintain anything in prod, they're a must.

I would understand the view of some people thinking "why even use python then" but that's a whole other conversation.

Type annotations are generally great, if you've never had the displeasure of working with Python pre type-annotations, you'd know that tracking down "wtf type even is this" was a big part of just working in Python. The extra text/tokens spent on annotations are 100 % worth it, both for humans and LLM.

An issue you run into these days is that even if you have excellent type checking on your own code, some libraries don't. You of course want to avoid such libraries, or see if you can find type stubs. Gradual typing can suck, but well-typed Python is a bliss.

I work with Python today without type annotations, don't find that kind of type tracking difficult at all, nor do I find the annotations worth the effort. The questions I've seen over the years, meanwhile, from people trying to force the annotation system to do strange things, have suggested to me that people are creating far too much complexity for themselves. Incidentally, IMX, ChatGPT usually writes un-annotated new code unless you specifically ask for annotations. And it can easily refactor that un-annotated code, too.

Annotations are ignored by the compiler (except insofar as they require adding some attributes to objects) and do not guide the bytecode-compilation process or empower any kind of optimization (without a dedicated third-party system like mypyc). Nor are they relevant to PyPy. They are not even free abstractions: as mentioned some objects gain additional attributes (in case you want to introspect something at runtime, but you usually don't), and e.g. calls to `typing.cast` still have to be looked up and made even though it's explicitly intended as a no-op. Plus, expecting annotations to reflect types that can be statically checked gets in the way of other interesting uses of annotations (and the original PEP explicitly designed with other use cases in mind).

I have been using Python for over 20 years. If anything, my attitude has become further against type annotations in the last few.

I routinely run into issues, where Claude will try to add functionality to a dict-heavy API and messup the keys, I just do an initial pass converting everything to dataclasses, and it can iterate on it's own with the typechecker to fix everything on it's own, but otherwise I have to do a bunch of manual iterations.
If it works for you, great. I'd just suggest trying `TypedDict` first; that's probably less disruptive.
The other great thing about type annotations is that you dont have to use them. Coming from strongly typed language background its life changing for me to sometimes rely on them sometimes just ignore.