Live data from Hacker News

Python developers are embracing type hints

pyrefly.org

21–30 of 581 posts

Re: Python developers are embracing type hints

#21

Type hints in dynamic languages are great, but I wish they came with deeper integration into the language runtime for validation and for optimizer setup. If I have a function that takes an int, and I write down the requirement, why should a JIT have to learn independently of what I wrote down that the input is an int? I get that it's this way because of how these languages evolved, but it doesn't have to stay this wa…

That was the original intent of mypy, to allow a subset of Python to be interpreted by a JIT or transpiled to a compiled, statically typed language.

The type hints proved to be useful on their own so the project moved past what was useful for that purpose, but a new JIT (such as the one the upcoming CPython 3.14 lays the groundwork for) could certainly use them.

Re: Python developers are embracing type hints

#22
post #8

Earlier quoted context omitted.

I worked on a project that did this. Drove me absolutely nuts. It's like having all the worst parts of a dynamic language and a static language with none of the benefits. I'd much rather just work in a statically typed language from the start.

What exactly drove you nuts? The python ecosystem is very broad and useful, so it might be suitable for the application (if not, reasonable that you'd be frustrated). With strict mypy/pyright settings and an internal type-everything culture, Python feels statically typed IME.

It's not even close compared to working with Java or Go or any language built with static typing in mind.

To be clear, I'm not opposed to type hints. I use them everywhere, especially in function signatures. But the primary advantage to Python is speed (or at least perceived speed but that's a separate conversation). It is so popular specifically because you don't have to worry about type checking and can just move. Which is one of the many reasons it's great for prototypes and fucking terrible in production. You turn on strict type checking in a linter and all that goes away.

Worse, Python was not built with this workflow in mind. So with strict typing on, when types start to get complicated, you have to jump through all kinds of weird hoops to make the checker happy. When I'm writing code just to make a linter shut up something is seriously wrong.

Trying to ad typing to a dynamic language in my opinion is almost always a bad idea. Either do what Typescript did and write a language that compiles down to the dynamic one, or just leave it dynamic.

And if you want types just use a typed language. In a production setting, working with multiple developers, I would take literally almost any statically typed language over Python.

Re: Python developers are embracing type hints

#25
The thing that finally got me on board with optional type hints in Python was realizing that they're mainly valuable as documentation.

But it's really valuable documentation! Knowing what types are expected and returned just by looking at a function signature is super useful.

Re: Python developers are embracing type hints

#26

I recently had to debug someone else's Python code and trying to figure out what variables are was a massive headache, especially coming from C++.

In Python, every variable is either defined or imported in the file in which it's used, so you always know where to find it. (Assuming you don't do `from foo import *`, which is frowned upon.)

In C++, a variable might be defined in a header or in a parent class somewhere else, and there's no indication of where it came from.

Re: Python developers are embracing type hints

#27
post #8

Earlier quoted context omitted.

I worked on a project that did this. Drove me absolutely nuts. It's like having all the worst parts of a dynamic language and a static language with none of the benefits. I'd much rather just work in a statically typed language from the start.

What exactly drove you nuts? The python ecosystem is very broad and useful, so it might be suitable for the application (if not, reasonable that you'd be frustrated). With strict mypy/pyright settings and an internal type-everything culture, Python feels statically typed IME.

I would say mypy is better than nothing but it still misses things sometimes, and makes some signatures difficult or impossible to write. I use it anyway, but patched-on static typing (Erlang, Clojure, and Racket also have it) seems like a compromise from the get-go. I'd rather have the type system designed into the language.

Re: Python developers are embracing type hints

#28
post #22

Earlier quoted context omitted.

What exactly drove you nuts? The python ecosystem is very broad and useful, so it might be suitable for the application (if not, reasonable that you'd be frustrated). With strict mypy/pyright settings and an internal type-everything culture, Python feels statically typed IME.

It's not even close compared to working with Java or Go or any language built with static typing in mind. To be clear, I'm not opposed to type hints. I use them everywhere, especially in function signatures. But the primary advantage to Python is speed (or at least perceived speed but that's a separate conversation). It is so popular specifically because you don't have to worry about type checking and can just move.…

But TypeScript erases (its) types at runtime, exactly like Python. Python is Python's TypeScript. Whether you want TS or JS-like semantics is entirely dependent on whether you use a type checker and whether you consider its errors a build breaker.

Re: Python developers are embracing type hints

#29
post #8
post #6

I enforce strong types on all Python code I’m responsible for - and make sure others don’t play fast and loose with dict[str, Any] when they could use a well defined type. Doing otherwise is just asking for prod incidents.

I worked on a project that did this. Drove me absolutely nuts. It's like having all the worst parts of a dynamic language and a static language with none of the benefits. I'd much rather just work in a statically typed language from the start.

I too would much rather work in a statically typed language, but sometimes you have to work with what you’ve got.

These systems are part of the core banking platform for a bank so I’d rather some initial developer friction over runtime incidents.

And I say initial friction because although developers are sometimes resistant to it initially, I’ve yet to meet one who doesn’t come to appreciate the benefits over the course of working on our system.

Different projects have different requirements, so YMMV but for the ones I’m working on type hints are an essential part of ensuring system reliability.

Re: Python developers are embracing type hints

#30
post #25

The thing that finally got me on board with optional type hints in Python was realizing that they're mainly valuable as documentation. But it's really valuable documentation! Knowing what types are expected and returned just by looking at a function signature is super useful.

Decent argument in principle. It still sucks for non-obvious types though:

https://old.reddit.com/r/Python/comments/10zdidm/why_type_hi...

Edit: Yes, one can sometimes go with Any, depending on the linter setup, but that's missing the point, isn't it?

Post reply on HN