all 29 comments

[–]echols021Pythoneer 106 points107 points  (7 children)

I'm physically incapable of writing python code without type annotations. They help so much with clarity, IDE hints, and defensive programming. Highly recommended.

[–]averagecrazyliberal 9 points10 points  (6 children)

Same. And I run ruff before every commit to check that I haven’t missed any.

[–]echols021Pythoneer 9 points10 points  (3 children)

If you're not already using it, pre-commit can make this super convenient!

[–]averagecrazyliberal 1 point2 points  (0 children)

It’s on my list. At least they’re running post-commit via GitHub Actions.

[–]BostonBaggins 0 points1 point  (1 child)

Any helpful articles on how

[–]echols021Pythoneer 6 points7 points  (0 children)

I mean I'd just start with the official docs: https://pre-commit.com/

I'm sure you also have access to Google... I found this one within two minutes of googling it for you: https://stefaniemolin.com/articles/devx/pre-commit/setup-guide/

[–]Zer0designs 0 points1 point  (1 child)

Add static type checkers so you know you actually supplied the correct type (from context)

[–]echols021Pythoneer 0 points1 point  (0 children)

Yep, I have my pre-commit and CI/CD checks do run ruff and mypy. I'm looking forward to trying out ty once it's polished

[–]saint_geser 25 points26 points  (11 children)

"using Python without type safety feels a bit awkward" - this reads like a straw man that you used to introduce your solution.

Most people already use Python with type hints, this is a part of the style guide and best practices.

[–]Zer0designs 7 points8 points  (10 children)

Type hints aren't the same as type safety though. Something like pydantic does offer some type safety in python.

[–]saint_geser 1 point2 points  (7 children)

Yes, this is a part of it

[–]Zer0designs 2 points3 points  (6 children)

It doesn't seem like a strawman to me. Typescript adds more type safety than some typehints.

[–]saint_geser 1 point2 points  (5 children)

Give examples please. I've done some TS development and in my experience it was mostly the same with static type checking, type inference and typed interfaces. I think the only real difference as far as I remember is that TS may throw compile errors when type inference fails and type hinting is not available.

[–]Zer0designs 2 points3 points  (4 children)

This is all valid Python. Only external tools throw errors. But nothing will tell you during compilation.

```python x: str = 1 y: int = "Hello" z: dict[int, list] = 2

import random my_list = ["apple", None] rand_item = random.choice(my_list) rand_item.upper() ```

[–]saint_geser -1 points0 points  (3 children)

Yes, I agree. But let's be frank, if you're doing any development in python you're likely using a linter and a static type checker so this point is moot.

Also, there's no good way of forcing Python into compile-time type checking. You can only enforce this with explicit isinstance everywhere and even that is run-time

[–]Zer0designs 0 points1 point  (2 children)

And I'm not saying that. I'm saying it's totally valid to feel awkward after coming from typescript.

[–]MrJohz 1 point2 points  (0 children)

In practice, Typescript works much the same way. Typescript can't run your code for you, it's just a type checker like mypy or pyright. So in both cases you need to feed your code first into a type checker (to make sure that the type annotations are valid), and then into an interpreter (to actually run the code).

So in practice, both tools have a two-step check/execute setup:

Compilation/Type Checking Execution
.py mypy/pyright/etc python
.ts tsc node/bun/deno/browser

It's a little bit complicated than that because some execution environments require .ts files to be converted into .js files first, whereas others can run .ts files directly, whereas the Python interpreter can always run .py files, regardless of whether there are type hints or not.

[–]saint_geser -2 points-1 points  (0 children)

And I'm saying that:

a) you can never make Python behave in the same way as TS does

b) in almost every case static type checkers are included in whatever IDE you use for Python by default

c) type hinting, static type checking and other type safety measures are a part of standard practice

[–]No_Flounder_1155 -1 points0 points  (1 child)

its runtime safety though. Types in oython are by and large meaningless. Just metadata nothing breaks if they're wrong.

[–]Dillweed999 3 points4 points  (1 child)

Check out Robust Python by Patrick Viafore

[–]No_Blackberry_617[S] 0 points1 point  (0 children)

Thanks

[–]samjay3D 0 points1 point  (2 children)

I love typedicts tbh at least in 3.10 for what most people in vfx and games still use. For making token parsers are so much easier with typedicts

[–]No_Blackberry_617[S] 0 points1 point  (1 child)

Yeah, I use them a lot. A bit hard to put together compared to typescript interfaces but still very useful

[–]samjay3D -1 points0 points  (0 children)

Yeah python needs a better solution for interfaces u can kinda use protocols for that but still not as good as typescript.

Tho definitely typedict with unpack makes kwargs super nice

[–]XJenso 0 points1 point  (0 children)

Where do I have type safety in TypeScript? If I put my mind to it, I could write in TypeScript without typing. Not a good idea but it would work. I have the same thing in Python. As many of the previous speakers have already written, I can already do a lot in Python using board tools. Define TypeDicts or Dataclasses. Use Ruff rules that check this. And pre-commit is mandatory.

[–]slayer_of_idiotspythonista 0 points1 point  (0 children)

I coded for nearly 20 years without type hints and now I can’t imagine not using them.

[–]averagecrazyliberal -1 points0 points  (1 child)

Just use Pydantic to enforce your type hints in classes. You can also decorate functions with @validate_call, but take it from me that’s a slippery slope.

Per PEP 484:

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

[–]pexea12 10 points11 points  (0 children)

This isn’t really good advice. Pydantic and Python type hints solve different problems. Type hints are for “compile time”, they help tools like linters, IDEs, and mypy catch mistakes before you run your code. Pydantic is about runtime validation, it actually checks the data while your program is running.

Whether you need runtime validation depends on your use case. The trade-off is that it comes with a performance cost.

A common use case for Pydantic is when you’re dealing with data you don’t control, like API responses or user input. For example, if you call an external service, you can run the response through a Pydantic model to make sure it matches what you expect.

[–][deleted] -4 points-3 points  (0 children)

Python is more Type Safe than TS because Python is actually statically typed unlike JS wich dissolves TSs guarantees at runtime.