Live data from Hacker News

Python developers are embracing type hints

pyrefly.org

241–250 of 581 posts

Re: Python developers are embracing type hints

#241
post #237

I actually don’t like python type hints! At my work we have a jit compiler that requires type hints under some conditions. Aside from that, I avoid them as much as possible. The reason is that they are not really a part of the language, they violate the spirit of the language, and in high-usage parts of code they quickly become a complete mess. For example a common failure mode in my work’s codebase is that some func…

They don’t violate the spirit of the language. They are optional. They don’t change the behaviour at runtime. Type annotations can seem pointless indeed if you are unwilling to learn how to use them properly. Using a giant union to type your (generic) function is indeed silly, you just have to make that function generic as explained in another comment or I guess remove the type hints

> They don’t violate the spirit of the language. They are optional.

That in itself violates the spirit of the language, IMO. “There should be one obvious way to do it”.

Re: Python developers are embracing type hints

#242
post #241
post #237

Earlier quoted context omitted.

They don’t violate the spirit of the language. They are optional. They don’t change the behaviour at runtime. Type annotations can seem pointless indeed if you are unwilling to learn how to use them properly. Using a giant union to type your (generic) function is indeed silly, you just have to make that function generic as explained in another comment or I guess remove the type hints

> They don’t violate the spirit of the language. They are optional. That in itself violates the spirit of the language, IMO. “There should be one obvious way to do it”.

the whole language violates this principle tbh, so it's very in spirit

Re: Python developers are embracing type hints

#243
post #240

I actually don’t like python type hints! At my work we have a jit compiler that requires type hints under some conditions. Aside from that, I avoid them as much as possible. The reason is that they are not really a part of the language, they violate the spirit of the language, and in high-usage parts of code they quickly become a complete mess. For example a common failure mode in my work’s codebase is that some func…

No idea about Python type system, but doesn't it have anything like this? interface IntIndexable { [key: number]: any }

It does!

You can specify a protocol like this:

  class IntIndexable(Protocol[T]):
    def __getitem__(self, index: int, /) -> T: ...
(Edit: formatting)

Re: Python developers are embracing type hints

#244
post #131

Earlier quoted context omitted.

As the article says, type hints represent a fundamental change in the way Python is written. Most developers seem to prefer this new approach (especially those who’d rather be writing Java, but are stuck using Python because of its libraries). However it is indeed annoying for those of us who liked writing Python 2.x-style dynamically-typed executable pseudocode. The community is now actively opposed to writing that…

There is nothing python-2 about my python-3 dynamically typed code. I'm pretty confident a majority of new python code is still being written without type hints. Hell, python type annotations were only introduced in python 3.5, the language was 24 years old by then! So no, the way I write python is the way it was meant to be, type hints are the gadget that was bolted on when the language was already fully matured, it…

I agree completely! To be clear, I don’t consider describing code as “Python 2-style” to be a bad thing. It’s how I describe my own Python code!

Overall, I have found very few Python 3 features are worth adopting (one notable exception is f-strings). IMO most of them don’t pull their weight, and many are just badly designed.

Re: Python developers are embracing type hints

#245
post #132

Earlier quoted context omitted.

Next stop is to agree that JSON is really NOT the semantic data exchange serialization for this "properly typed" world.

Then what is? Everybody knows the limitations of JSON. Don't state the obvious problem without stating a proposed solution.

The RDF structure is a graph of typed instances of typed objects, serializable as text.

Exchanging RDF, more precisely its [more readable] "RDF/turtle" variant, is probably what will eventually come to the market somehow.

Each object of a RDF structure has a global unique identifier, is typed, maintains typed links with other objects, have typed values.

Re: Python developers are embracing type hints

#246

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.

Well, we do coalesce on certain things... some static type languages are dropping type requirements (Java and `var` in certain places) :D

Re: Python developers are embracing type hints

#247
post #245

Earlier quoted context omitted.

Then what is? Everybody knows the limitations of JSON. Don't state the obvious problem without stating a proposed solution.

The RDF structure is a graph of typed instances of typed objects, serializable as text. Exchanging RDF, more precisely its [more readable] "RDF/turtle" variant, is probably what will eventually come to the market somehow. Each object of a RDF structure has a global unique identifier, is typed, maintains typed links with other objects, have typed values.

For an example of RDF being exchanged between a server and a client, you can test

https://search.datao.net/beta/?q=barack%20obama

Open your javascript console, and hover the results on the left hand side of the page with your mouse. The console will display which RDF message triggered the viz in the center of the page.

Update: you may want to FIRST select the facet "DBPedia" at the top of the page, for more meaningful messages exchanged.

Update2: the console does not do syntax higlighting, so here is the highlighted RDF https://datao.net/ttl.jpg linked to the 1st item of " https://search.datao.net/beta/?q=films%20about%20barack%20ob... "

Re: Python developers are embracing type hints

#248
I liked python in my early days because it felt simple and easy and when I tried other languages having to deal with types felt so annoying... but then I grew and had to work with bigger codebases and guess what - having types (and static type checking during compilatin) helps A LOT... :)

Re: Python developers are embracing type hints

#249
post #241
post #237

Earlier quoted context omitted.

They don’t violate the spirit of the language. They are optional. They don’t change the behaviour at runtime. Type annotations can seem pointless indeed if you are unwilling to learn how to use them properly. Using a giant union to type your (generic) function is indeed silly, you just have to make that function generic as explained in another comment or I guess remove the type hints

> They don’t violate the spirit of the language. They are optional. That in itself violates the spirit of the language, IMO. “There should be one obvious way to do it”.

Well, precisely:

- There is one obvious way to provide type hints for your code, it’s to use the typing module provided by the language which also provides syntax support for it.

- You don’t have to use it because not all code has to be typed

- You can use formatted strings, but you don’t have to

- You can use comprehensions but you don’t have to

- You can use async io, but you don’t have to. But it’s the one obvious way to do it in python

The obvious way to annotate a generic function isn’t with a giant Union, it’s with duck typing using a Protocol + TypeVar. Once you known that, the obvious way is… pretty obvious.

The obvious way not be bothered with type hints because you don’t like them is not to use them!

Python is full of optional stuff, dataclasses, named tuples, meta programming, multiple ancestor inheritance. You dont have to use these features, but there are only one way to use them

Re: Python developers are embracing type hints

#250

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.

The need for typing changed, when the way the language is used changed.

When JavaScript programs were a few hundred lines to add interactivity to some website type annotationd were pretty useless. Now the typical JavaScript project is far larger and far more complex. The same goes for python.

Post reply on HN