News for hackers. Stuff that matters.
Re:Python type hints may not be not for me in practice by Myrmornis on Wednesday November 27, 2024 @09:17AM (#42256253)
Can you give some examples of how the Python type system is disappointing you?
Re:Python type hints may not be not for me in practice by colemannerd on Wednesday November 27, 2024 @04:15PM (#42259849)
default values! Since type hints are *hints*, it is difficult to set default values for complicated types. For instance, if you have lists, dicts, sets in the type signature, without a library like pydantic, it is difficult and non-standard. This becomes even more problematic when you start doing more complicated data structures. The configuration in this library starts to show the problems. https://koxudaxi.github.io/datamodel-code-generator/custom_t...

The issue very much is a lack of a standard for the entire language; rather than it not being possible.

Re:Python type hints may not be not for me in practice by alfons_foobar on Wednesday November 27, 2024 @04:32PM (#42259991)
I might be dense, but I don't understand what that has to do with type hints...

To my eyes, the problem of choosing useful defaults for complicated types/datastructures is independent of whether I add type hints for them.

I think I am missing something...

Re:Python type hints may not be not for me in practice by CJefferson on Wednesday November 27, 2024 @04:35PM (#42260012)
Mainly, the seems to be no way, in a dynamic language, to dynamically check if functions get the right types.

To me, this means I don't really understand the python type hinting at all, as adding hints to just one or two functions provides no value to me at all.

I assume I must be not using them usefully, as I've tried adding type hints to some projects and they just seemed to do nothing useful.

Re:Python type hints may not be not for me in practice by patrickkidger on Wednesday November 27, 2024 @05:08PM (#42260293)
You want runtime typechecking.

See either beartype [1] or typeguard [2]. And if you're doing any kind of array-based programming (JAX or not), then jaxtyping [3].

[1] https://github.com/beartype/beartype/

[2] https://github.com/agronholm/typeguard

[3] https://github.com/patrick-kidger/jaxtyping

Re:Python type hints may not be not for me in practice by daelon on Wednesday November 27, 2024 @07:16PM (#42261156)
Thanks for posting this. I had seen beartype several years ago but I don't believe it had the whole-module registration feature yet. I'm looking forward to trying both of the libraries since the ergonomics are better than decorating every function individually.
Re:Python type hints may not be not for me in practice by Spivak on Wednesday November 27, 2024 @04:41PM (#42260072)
Type hints alone don't do this, but you can use Pydantic to accomplish what you want. In Python type hints aren't enforced anywhere at runtime. They're for a type-checker to validate your source.

https://docs.pydantic.dev/latest/concepts/validation_decorat...

Re:Python type hints may not be not for me in practice by zo1 on Wednesday November 27, 2024 @06:10PM (#42260725)
How to tell me you use VScode without telling me you use VScode.
Re:Python type hints may not be not for me in practice by Myrmornis on Friday November 29, 2024 @05:34PM (#42277739)
It's a static type-checking system, along the same lines as what Rust, Java, C++ etc have during their compilation processes (and what Typescript has during its transpilation step). The main purpose of static type-checking systems is to find bugs before you ever run the code. It's most useful if your editor shows the type errors as you type, but it can also be done by running pyright or mypy from the command line. And of course running pyright or mypy in CI to guarantee type errors don't get into main. But you are right that in Python they are optional and the value increases with the number of annotations you add!
Re:Python type hints may not be not for me in practice by sevensor on Wednesday November 27, 2024 @09:47PM (#42261958)
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.