top | item 29804496

(no title)

guiand | 4 years ago

I swear by type hinting in Python, but it is pretty frustrating how many common patterns don’t work or require very convoluted use of typing.

It’s an uphill battle to convert a team over to using hinting because of how awkward things can get and how easy it is to just pretend the feature doesn’t exist.

discuss

order

wraptile|4 years ago

The problem is that people try to turn type hints into static types. I got rid of mypy from all of my projects as it provided no value and just created this constant battle of "making it work" - a collosal time waste. As this blog illustrates - so much extra effort for something so trivial and unimportant - this whole shebang goes against the spirit of Python.

In general I've yet to see what these type check systems (be it mypy, pyright or any other) offer over proper tests. Type hints are there for developers not the machine.

ameminator|4 years ago

I'm going to hard disagree with you here. Type hints are a key tool in working on a large Python project in a large team. Tests are, of course, necessary. But type hints allow Python (and all its benefits) to be used in industrial environments where it otherwise would be intractable.

lmm|4 years ago

> In general I've yet to see what these type check systems (be it mypy, pyright or any other) offer over proper tests. Type hints are there for developers not the machine.

In a language with proper first-class typing, the advantage is that they're included in your automated refactoring rather than needing manual updates. But yeah I've never found optional type systems to be any use.

BerislavLopac|4 years ago

> people try to turn type hints into static types

The only way for type hints not to be static types is to not run the checker before deploying.

Static typing means that the types of variables and their values are statically checked (hence the name) before the execution; in most statically typed languages that happens at compile time. There is no static check at runtime, and it's perfectly possible to send an incorrect type to a dynamically loaded library even in compiled languages (which usually, but not necessarily, results in crashing he program).

toomanydoubts|4 years ago

Python sucks, type hints border idiocy. If you can't trust the hint to actually be correct and still have to check for yourself, they become pointless.

If you want to see where static typing really shines, try writing some Haskell. It's an absolute delight.

Spivak|4 years ago

Yeah, it’s always awkward to have to declare a variable or add an assert or a weird cast thing to make the type checker happy. Basically any case where type hints have to be evaluated at runtime for syntactic correctness.

The import pattern in the article is actually something I’ve never encountered because of how common the HAS_MODULE pattern is. I’ve never even thought to override the module binding.