Live data from Hacker News

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

uninformativ.de

21–30 of 597 posts

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

#21

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.

They're great for autocomplete, but that's pretty much it

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

#22

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.

> If you want types, use something else other than Python.

Python is still strongly typed.

Me: what type does this argument need to be?

Python: you have to figure that out

Me: I just pass whatever I want?

Python: Oh no. Usually only a single specific type is supported. But you have to guess what it is.

Me: what if I get it wrong?

Python: the program crashes at runtime

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

#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? MyPy occasionally catches issues with my types and saves me some work. If a type is very complex, I won't bother spending time annotating. The end result is that I have some code that is annotated and prevents simple errors, and other code that isn't, and I didn't spend much time or effort doing it. To me, that's a clear net win.

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

#24
post #8

I tried mypy when I first started into type checking in Python back when I was trying to do Python in VS Code and I discovered that mypy is a time-sucking anti-productivity disaster that needs to die in a fire. When I started learning PyCharm though, I discovered that type checking can actually work well in Python and it is not a waste of time at all. So I advise people now that unless you're using PyCharm, do not wa…

If you use VSCode and Pylance (Microsoft python language server) you don’t need to setup Mypy, you can enable type checking in user settings and it will use its own engine for type analysis. You have two modes, basic and strict.

I would recommend to try out if you can, the experience is quite good. I personally find “strict” too strict for the current state of python, but “basic” is already very helpful.

The developer experience is still subpar when compared to Typescript but it’s improving fast (but I mean, typescript with VSCode has one of the best developer experience I’ve ever seen).

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

#25
post #4

It reminds me of this 5 year old bug in mypy: "int is not a Number?" [1]. [1]: https://github.com/python/mypy/issues/3186

It isn't really one though (a lot of reading):

https://stackoverflow.com/questions/69334475/how-to-hint-at-...

I would rather my type checker give me the truth than gloss over it. This issue should be kept open (it is) but they should wait until a good solution is found that doesn't gloss over the discrepancy.

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

#26
post #8

I tried mypy when I first started into type checking in Python back when I was trying to do Python in VS Code and I discovered that mypy is a time-sucking anti-productivity disaster that needs to die in a fire. When I started learning PyCharm though, I discovered that type checking can actually work well in Python and it is not a waste of time at all. So I advise people now that unless you're using PyCharm, do not wa…

Could you elaborate on mypy? I'm not a Python developer (and I really have much more experience with statically typed languages), but if I ever had to maintain a Python codebase, I'd assume that adding mypy and type annotations would be among the first things I'd do, so it's a bit of a surprise to read that.

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

#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 when you split() on an integer. It does introduce a significant amount of overhead, but it's still useful during development alongside type hints to avoid some of the pitfalls from this article.

In another vein, Any should definitely be used sparingly. For the foo() function outlined in the article, Union[str, int] offers more precision. In fact, you could even argue that using Any is a code smell.

Overall, though, I agree with most of the sentiment in this article. I find myself using type hints more as documentation than anything else, since their true ability to prevent type-related runtime bugs is very limited. Documentation has the same qualms the author outlines, anyway:

> You can not be sure that they are correct. [...] They waste mental energy when reading code, they create new maintenance burdens, and they are potentially deceiving: You cannot trust them.

But they're still slightly better than docstrings...

[1]: https://typeguard.readthedocs.io/en/latest/userguide.html

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

#28
Yeah they're pretty bad but they are still better than nothing. If you are in the unfortunately position of having to use Python I would recommend using them.

I would also strongly recommend using Pyright (the default checker in VSCode) over MyPy. It's so much better, and the author is really responsive on Github.

But yeah, in general trying to use type hints in Python is like trying to discuss philosophy in a mental asylum.

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

#29
Type hints work fine. They are hints (that are checkable through tools)

> But as a general rule: You don’t know if there really is a tool in place to check them.

Cool. So you just go and do stupid stuff if no one is looking?

Check it yourself. They're probably there for a reason. If you think they're not needed, then sure, take them out.

> Most of this turned out to be wrong and it threw me off the track during my debugging session.

Here's an idea: Then why don't you fix it. If I see code that's wrong or bad, I go and fix it. It's your responsibility as well

Now maybe try acting like they're anything but type hints (and I'm very glad Guido insisted it is optional and flexible - the last thing the world needed is for Python to get consumed by Java type checking pedantry where the compiler needs you to annotate every single thing)

And every time I do something in Python that would be "impossible" in Java I'm glad I'm not bound by the small-minded type pedantry of Java.

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

#30
I find it reasonable that Python doesn't enforce the type checking. The owners of each project can choose how much to enforce it and how.

Defining such style rules and validations is necessary with almost every language. A codebase where developers are allowed to use any C++ feature or browser API will become very messy as it grows.

Post reply on HN