Comment by baq

2 months ago

Good that Python supports types then

Python has formalized magic comments.

You can run a third party linter on those comments, but you must hope that they're correct. There are usually some checks for that, but they're only reliable in trivial cases.

This is not static typing any more than "you can use emscripten to transpile JavaScript to C" means that JavaScript is a low level language with native assembly support. It's a huge step forward from "no system at all" and I'm thrilled it exists, but it's hardly the same thing.

  • They are originally magic comments, unformalized. The feature is originally known as annotations and you can basically put anything there.

        def f(a: "something like an int", b: "another int-like thing"): pass
    
        def g(a: sys.stdin, b: sys.stderr): pass

It's actually remarkable how with the success of TypeScript so many other dynamic languages switched to gradual typing.

Erlang and Clojure were the early ones, TypeScript followed, and now Python, Ruby, and even Perl have ways to specify types and type check your programs.

> Good that Python supports types then

"Optional typing" is not the same as "Static typing".

Great, my program will crash, because I forgot to opt-in to typing :-/

Poorly, though, and with lots of edge cases and foot guns to make you miserable once your code calls out to numpy or gets some JSON.

Tell me more please: how does one use types in Python? Unfortunately I write Python professionally these days (it is the language that has all the libraries) and hate it with a passion.

  • You will never get good if you continue to hate it. You don't have to like it, but hating it creates a mental block.

  • mypy is the simplest/oldest option, look into pyright and ty, too.

    install uv and then

        uvx mypy
    

    should do the rest.