Live data from Hacker News

Python type hints may not be not for me in practice

utcc.utoronto.ca

131–140 of 209 posts

Re: Python type hints may not be not for me in practice

#131
post #79

Earlier quoted context omitted.

> when types weren't an option we weren't going towards the cliff Erm yes we were. Untyped Python wasn't magically tolerable just because type hints hadn't been implemented yet.

How come all those unicorns were built with intolerable Python/Ruby, not Java/C#/Go? https://charliereese.ca/y-combinator-top-50-software-startup...

My previous unicorn rose despite the initial tech, not because of it

Re: Python type hints may not be not for me in practice

#132

The problem (in my opinion) is that Python gives you the tools (and perhaps even encourages you) to write code that would benefit from typing. It's perfectly feasible to write maintainable, well-designed code in a dynamic language. I've worked with some extremely robust and ergonomic Clojure codebases before, for example. However, in Clojure, the language pushes you into its own "pit of success". Personally, I never…

That’s good to hear about clojure! I just started learning the language and it’s been a ton of fun (especially babashka omg) but I’m so typescript-minded that it’s been really tough being back in dynamic land.

Have you looked into core.spec at all? It's been a while since I've even thought about it but I believe it's an interesting middle ground between Python's type hints and TypeScript static typing. It functions as sort of verifiable documentation (at runtime, if you wish) and can also be used to dynamically generate data (e.g. for testing).

I miss Clojure.

EDIT: Here's a great talk Rich Hickey gave about it at LispNYC, which I was lucky enough to attend.

https://youtu.be/dtGzfYvBn3w?feature=shared

Re: Python type hints may not be not for me in practice

#134
post #21

The Python type system is pretty bad, but it's still 100x better than not using types. We are heavy users of the (Rust) type system at Svix, and it's been a godsend. I wrote about it here https://www.svix.com/blog/strong-typing-hill-to-die-on/ We also use Python in some places, including the shitty Python type-system (and some cool hackery to make SQLAlchemy feel very typed and work nicely with Pydantic).

Can you give some examples of how the Python type system is disappointing you?

As a heavy user of Python’s type annotations, I’m very happy with them, but I would like for them to be first class at runtime, so I can do useful and interesting things with them. The status quo is that a type annotation can be a class, a string, or a “typing special form.” I would like for a type annotation to be an object that could exist independently and be treated as a value, and this is only sometimes true.

Re: Python type hints may not be not for me in practice

#135
post #129
post #120

The biggest issue with Python type hints for me isn't the hints themselves, it's that they encourage people to write overly complex, verbose code just to satisfy the type checker. Code like this [0] could simply be 3 functions. Instead it's 3 classes, plus a base class `AstNode`, just so the author can appease the type checker by writing `body: List[AstNode]` instead of the dynamically-typed `body = []`. [0] https://…

That code looks like a proper object-oriented design to me, nothing to do with type-hints actually.

Type hints encourage this sort of object-oriented design though, in my experience. The resulting code is extremely verbose compared to Pythonic "executable pseudocode".

For example, see Jack Diederich's talk "Stop Writing Classes": https://www.youtube.com/watch?v=o9pEzgHorH0

Re: Python type hints may not be not for me in practice

#136
post #63

Earlier quoted context omitted.

> Writing software without types lets you go at full speed. Full speed towards the cliff. Isn't it strange that back when Python (or Ruby) didn't even have type hints (not type checkers, type hints!), it would easily outperform pretty much every heavily typed language? Somehow when types weren't an option we weren't going towards the cliff, but now that they are, not using them means jumping off a cliff? Something do…

Every single typed system I have ever worked on, no matter how poorly designed, has been easier to alter than the vast majority of ruby, python, perl, php, and elixir that I've worked on

I have the opposite experience:

Inserting a library that wraps an existing one to add new features has been a nightmare in every statically typed language I’ve used — including times it’s virtually impossible because you’d need the underlying library to understand the wrapper type in its methods.

In Python (with duck typing), that’s a complete non-issue.

Re: Python type hints may not be not for me in practice

#137
post #135
post #129

Earlier quoted context omitted.

That code looks like a proper object-oriented design to me, nothing to do with type-hints actually.

Type hints encourage this sort of object-oriented design though, in my experience. The resulting code is extremely verbose compared to Pythonic "executable pseudocode". For example, see Jack Diederich's talk "Stop Writing Classes": https://www.youtube.com/watch?v=o9pEzgHorH0

That talk had a big impact on my coding style. But citing a 99 line script written as an example for a blog post doesn't really support your argument. 99 lines is short, and verbosity is expected in such example code.

Consider FastAPI. It uses functions as endpoints, like flask. Very compatible with "Stop Writing Classes." It also leverages type hinting to eliminate boilerplate and create more concise code. You don't have to put validation or dependency injection logic at the top of every endpoint, it's handled for you so you can dedicate screen space to the problems you're solving.

Consider also the pythonism, "explicit is better than implicit." If memory serves, "Stop Writing Classes" wasn't so much about not writing containers for data but not writing containers for behavior when it wasn't associated with data. Behavior can live as a freestanding function just as well as inside of an object. But it's difficult to understand the semantics of freestanding nontrivial data, like dictionaries or long tuples.

Dataclasses and pydantic models require a minimum of boilerplate and couple the data with it's semantic meaning, so that it's preserved across boundaries. I for one am never going back to the Python before these tools.

Re: Python type hints may not be not for me in practice

#138
post #39

Earlier quoted context omitted.

> The logic of type hint is not bad but sadly I think that type hint are making python source code messy and unreadable. Compared to legacy Python, yes. Compared to verbose language like Java, no. Python typing is equal or less verbose than Java (unless you use "var" in Java).

Python people legitimately upset they can't write every function like this now: def func(data, *kwargs): """data: the data. kwargs: other data."""

# returns the result

Re: Python type hints may not be not for me in practice

#139
If you could snap your fingers and have your type hints update to match your code, it wouldn’t get in the way of your work.

Hyperbolically: You have to be able to edit code at the speed of thought - whatever it takes - or else programming languages cease to be a more useful tool than just thinking.

If you type slower than you think, or can’t do the type-hint-based textual translation as quickly as you think, then… yeah - it’s not good for you.

The advice I’d wanna hear for myself is: just get better. But the advice I’d give to my coworkers is: have explicit domains where you’re able to do whatever is most efficient and effective, and then when you hand off data to the next subsystem - obey a contract. A schema. Be that type hints or a .proto file or a database schema or an API. Doesn’t matter.

Re: Python type hints may not be not for me in practice

#140
The same applies to tests, maybe docs too.

For x in tests, type annotations, and documentation*:

If you write your x first then you have to decide what your API is. This is great if you want to think about your API. Sometimes though you just want to get down to it and play around with a new idea. Either way is fine.

As soon as you start sharing code or patching production code or patching someone else’s production code, one must insist on seeing some kind of x. Having x around the outside of a system — rather than requiring x be added throughout the entire system — is often good enough.

*The useful, architecture kind.

Post reply on HN