Live data from Hacker News

Python developers are embracing type hints

pyrefly.org

171–180 of 581 posts

Re: Python developers are embracing type hints

#171

As a static typing advocate I do find it funny how all the popular dynamic languages have slowly become statically typed. After decades of people saying it's not at all necessary and being so critical of statically typed languages. When I was working on a fairly large TypeScript project it became the norm for dependencies to have type definitions in a relatively short space of time.

In the discussion of static vs dynamic typing solutions like typescript or annotated python were not really considered.

IMHO the idea of a complex and inference heavy type system that is mostly useless at runtime and compilation but focused on essentially interactive linting is relatively recent and its popularity is due to typescript success

I think that static typing proponents were thinking of something more along the lines of Haskell/OCaml/Java rather than a type-erased system a language where [1,2] > 0 is true because it is converted to "NaN" > "0"

Re: Python developers are embracing type hints

#173

Python with type hints still lacks the performance benefits of static typing in a compiled language setting?

It's the developer performance benefit of catching type bugs early, not the application performance benefit from a compiler, that Python developers find compelling

Re: Python developers are embracing type hints

#174
post #108
post #29

Earlier quoted context omitted.

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 cou…

A statically typed language doesnt prevent developers from using the equivalent of dict[str, Any].

Well, some do. Let's not pretend all static type systems are the same.

Re: Python developers are embracing type hints

#175

Earlier quoted context omitted.

I learned C++ before learning python as well and python felt like a breath of fresh air. At first I thought it was because of the lack of types. But in actuality the lack of types was a detriment for python. It was an illusion. The reason why python felt so much better was because it had clear error messages and a clear path to find errors and bugs. In C++ memory leaks and seg faults are always hidden from view so EV…

> It was an illusion. We didn't like it more because of the lack of typing. These languages were embraced because they weren't C or C++. It's an illusion only you once had. Java (a language that is not C or C++) got mainstream way before Python.

I don't understand, the parent says that not being C/C++ was a strong point and you give an counter example of a successful language that is not C/C++

Re: Python developers are embracing type hints

#176

One can hope that cpython will use them, one day "if a parameter is typed as an int, then only run the specialized 'int' code to process it" This would increase performance and make typing more useful

I think you're forgetting that int is actually what other people call a BigInt, an integer with unlimited precision, not int32 or int64.

Re: Python developers are embracing type hints

#177

I know I am going to be in the minority, but I don't understand why we can't let Python be Python. Static typing is great, and there are already other statically typed languages for all your needs. Why not use them? Well, at least it doesn't create two incompatible Pythons like async and (I assume) free threading.

> Why not use them?

Because you can now use typing WITH the entire Python ecosystem.

Re: Python developers are embracing type hints

#178

Earlier quoted context omitted.

My love for python was critically hurt when I learned about typing.TYPE_CHECKING. For those unaware, due to the dynamic nature of Python, you declare a variable type like this foo: Type This might look like Typescript, but it isn't because "Type" is actually an object. In python classes and functions are first-class objects that you can pass around and assign to variables. The obvious problem of this is that you can…

You're actually missing the benefit of this. It's actually a feature. With python, because types are part of python itself, they can thus be programmable. You can create a function that takes in a typehint and returns a new typehint. This is legal python. For example below I create a function that dynamically returns a type that restricts a Dictionary to have a specific key and value. from typing import TypedDict def…

Are there really productive projects which rely on types as a proofing system? I've always thought it added too much complexity to the code, but I'd love to see it working well somewhere. I love the idea of correctness by design.

Re: Python developers are embracing type hints

#179

As a static typing advocate I do find it funny how all the popular dynamic languages have slowly become statically typed. After decades of people saying it's not at all necessary and being so critical of statically typed languages. When I was working on a fairly large TypeScript project it became the norm for dependencies to have type definitions in a relatively short space of time.

dynamically-typed languages were typically created for scripting tasks - but ended up going viral (in part due to d-typing), the community stretched the language to its limits and pushed it into markets it wasn't designed/thought for (embedded python, server-side js, distributed teams, dev outsourcing etc).

personally i like the dev-sidecar approach to typing that Python and JS (via TS) have taken to mitigate the issue.

Re: Python developers are embracing type hints

#180
post #77

I love typing in Python. I learnt programming with C++ and OOPs. It was freeing when I took up Python to note care about types, but I have come to enjoy types as I got older. But, boy have we gone overboard with this now? The modern libraries seem to be creating types for the sake of them. I am drowning in nested types that seem to never reach native types. The pain is code examples of the libraries don’t even show t…

> But, boy have we gone overboard with this now? The modern libraries seem to be creating types for the sake of them. I am drowning in nested types that seem to never reach native types. Thought you were talking about TypeScript for a moment there.

Except that typescript structural typing and features make it much easier to swim.

Also python is far less aggressive with lint warnings so it is much easier to make mistakes

Post reply on HN