Live data from Hacker News

Python’s “type hints” are a bit of a disappointment to me

uninformativ.de

31–40 of 597 posts

Re: Python’s “type hints” are a bit of a disappointment to me

#31
Yes, they lack teeth. But what’s the alternative?

* fork the language again? We just got out of Python2 hell.

* Docstrings, like we had before they were introduced?

They won’t catch all bugs, but with minimal effort they’ll catch some bugs, and if you make an effort to really dig in, it’ll catch most bugs. To me, to accomplish that overnight on a language with millions of existing lines, that’s a big win.

Re: Python’s “type hints” are a bit of a disappointment to me

#32

100% agree - never really understood the movement behind adding types to Python. Type hints are a useless complexity that yield little return. If you want types, use something else other than Python. The whole thesis behind Python is simple is better than complex.

I find it best to see type hinting as "living documentation". Yeah, you can ignore it, it is Python afterall, but if you want the IDE/tools to have better more helpful hints ... use machine readable hint documentation.

I'd argue if your functions are full of type-assert like guards, _then_ use another language!

Re: Python’s “type hints” are a bit of a disappointment to me

#33

100% agree - never really understood the movement behind adding types to Python. Type hints are a useless complexity that yield little return. If you want types, use something else other than Python. The whole thesis behind Python is simple is better than complex.

> useless complexity that yield little return

Typechecking allows certain errors to be detected at typecheck time rather than at runtime. I don't personally consider that useless. E.g. A mistake I just made. I'm working in a language I don't know too well right now with strict static typing. The file read function takes a handle. If I pass it a string of the file name, rather than the handle result from file open, it just doesn't compile. In most dynamic languages, that error would not be detected until it executes.

Re: Python’s “type hints” are a bit of a disappointment to me

#36
post #27

Despite this: > Even if the Python runtime did check all the type hints at runtime, then it would still be too late. I don’t want a fancy type exception at runtime. That already exists (most of the time). I want to know about type mismatches in advance. You should check out Typeguard [1], which lets you add a @type_checked decorator to anything and get runtime type checking that's far better than e.g. a random blowup…

There's also bear-type for runtime type checking: https://github.com/beartype/beartype.

Re: Python’s “type hints” are a bit of a disappointment to me

#37
post #23

Opinions like these are frustrating because, although they make valid points, it comes off as "they didn't do 100% exactly what I want, so I'm not using them at all." With many tools, there is a middle ground between using them for everything, and not using them at all. Python's type hints is one of those tools. When I'm writing code, and a variable or signature is easy to annotate, I'll annotate it. And guess what?…

Fully agree with this. Type hinting (not checking) is idiomatic to Python. MyPy is a great way to enforce checking. It's great to have the option to use the hints for intellisense only.

Re: Python’s “type hints” are a bit of a disappointment to me

#38
In my experience, people with less experience with object oriented programming will end up creating lots of objects only to create types. These objects have no business use case, nor do they aid in any encapsulation. These objects are made just to help typing. Nuts!

Python type hinting is like moving backwards in time because the amount of time devs take to "hint", takes away the main reason for using python, that is faster development. Might as well code in Java at this point.

Re: Python’s “type hints” are a bit of a disappointment to me

#39
> It feels really good to see a program compile without warnings or errors. You then know that you got it right.

I don't know about that. So many C/C++ programs have core dumped in my life and needed to be valgrinded to debug memory issues, or went past the bounds of a pointer, or overwrote the stack, or... whatever.

And you still don't get correctness without unit tests.

I'm not saying types aren't valuable, but they're not a crutch to program correctness like people seem to think they are.

Re: Python’s “type hints” are a bit of a disappointment to me

#40

this article is so full of... not-very-educated thoughts on gradual typing in a dynamic language that it's hard to know where to start. A few concrete criticisms: 1. If you're using a dynamic language, then _by definition_ the language will not enforce your static hints at runtime. However, good news! Python has always been strongly typed, and _does_ enforce types at runtime! 2. A number of the examples given would b…

That’s the point though:

Duck typing and heterogeneous dictionaries are (and have been) standard Python.

Adding a type system which doesn’t respect duck typing by trying to access the member even when the types don’t match or which can’t express standard idioms used in Python seems a poor architectural choice.

It’s not saying that Python’s type system is “too much discipline”, but rather that the type system doesn’t encode typical Pythonisms.

Post reply on HN