Live data from Hacker News

Python developers are embracing type hints

pyrefly.org

101–110 of 581 posts

Re: Python developers are embracing type hints

#101

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…

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.

Re: Python developers are embracing type hints

#102
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.

This is a naive realization. When type checking is used to the maximum extent they become as just as important as unit testing. It is an actual safety contribution to the code. Many old school python developers don't realize how important typing actually is. It's not just documentation. It can actually roughly reduce dev time by 50% and increase safety by roughly 2x.

> It can actually roughly reduce dev time by 50% and increase safety by roughly 2x.

Type annotations don’t double productivity. What does “increase safety by 2×” even mean? What metric are you tracking there?

In my experience, the main non-documentation benefit of type annotations is warning where the code is assuming a value where None might be present. Mixing up any other kind of types is an extremely rare scenario, but NoneType gets everywhere if you let it.

Re: Python developers are embracing type hints

#104

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…

That's not a benefit. That's a monstrosity.

And, as you heavily imply in your post, type checkers won't be able to cope with it, eliminating one if the main benefits of type hints. Neither will IDEs / language servers, eliminating the other main benefit.

Re: Python developers are embracing type hints

#105
post #23

Or you could just use a statically typed language and get a much better experience.

Yeah, I thought lack of typing in python was intentional to support this another paradigm of programming for geniuses who don't need hand holding.

Turns out they just didn't know any better?

Re: Python developers are embracing type hints

#106

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…

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…

This is trivial to solve by simply not having circular imports. Place the types in one file and the usage of it in others.

This has many benefits, like forcing you to think about the dependencies and layers of your architecture. Here is a good read about why, from F# that has the same limitation https://fsharpforfunandprofit.com/posts/cyclic-dependencies/

As others already mentioned, importing __annotations__ also works.

Re: Python developers are embracing type hints

#107

I was extremely skeptical when typing was introduced. What's the point if runtime ignores them. I forced myself to use them as documentation and now I'm on the other side of the spectrum, all code should have them. It already helped me a lot during refactors and I always wished more code had types. I think the same is true for AIs, they will benefit from having the types explicitly said. It's a shame they currently d…

> What's the point if runtime ignores them.

I've been using this sparingly: https://pypi.org/project/type-enforced/

Re: Python developers are embracing type hints

#108
post #29
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.

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].

Re: Python developers are embracing type hints

#110
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.

Post reply on HN