r/Python • u/No_Blackberry_617 • Aug 31 '25
Resource Python type system
(Just sharing something)
As someone who has taken advantage of TypeScript's type safety for most of its career, using Python without type safety feels a bit awkward. I put together a page explaining how to take advantage of Python's type system and how to extend it on your editor.
107
u/echols021 Pythoneer Aug 31 '25
I'm physically incapable of writing python code without type annotations. They help so much with clarity, IDE hints, and defensive programming. Highly recommended.
10
u/averagecrazyliberal Aug 31 '25
Same. And I run ruff before every commit to check that I haven’t missed any.
11
u/echols021 Pythoneer Aug 31 '25
If you're not already using it,
pre-commitcan make this super convenient!2
u/averagecrazyliberal Aug 31 '25
It’s on my list. At least they’re running post-commit via GitHub Actions.
1
u/BostonBaggins Aug 31 '25
Any helpful articles on how
7
u/echols021 Pythoneer Aug 31 '25
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/
1
u/Zer0designs Aug 31 '25
Add static type checkers so you know you actually supplied the correct type (from context)
1
u/echols021 Pythoneer Aug 31 '25
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
26
u/saint_geser Aug 31 '25
"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.
7
u/Zer0designs Aug 31 '25
Type hints aren't the same as type safety though. Something like pydantic does offer some type safety in python.
2
u/saint_geser Aug 31 '25
Yes, this is a part of it
3
u/Zer0designs Aug 31 '25
It doesn't seem like a strawman to me. Typescript adds more type safety than some typehints.
2
u/saint_geser Aug 31 '25
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.
3
u/Zer0designs Aug 31 '25 edited Aug 31 '25
This is all valid Python. Only external tools throw errors. But nothing will tell you during compilation.
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()0
u/saint_geser Aug 31 '25
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
isinstanceeverywhere and even that is run-time1
u/Zer0designs Aug 31 '25
And I'm not saying that. I'm saying it's totally valid to feel awkward after coming from typescript.
2
u/MrJohz Aug 31 '25
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 .ts tsc It's a little bit complicated than that because some execution environments require
.tsfiles to be converted into.jsfiles first, whereas others can run.tsfiles directly, whereas the Python interpreter can always run.pyfiles, regardless of whether there are type hints or not.-1
u/saint_geser Aug 31 '25
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
0
u/No_Flounder_1155 Aug 31 '25
its runtime safety though. Types in oython are by and large meaningless. Just metadata nothing breaks if they're wrong.
1
u/samjay3D Aug 31 '25
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
1
u/No_Blackberry_617 Aug 31 '25
Yeah, I use them a lot. A bit hard to put together compared to typescript interfaces but still very useful
0
u/samjay3D Sep 01 '25
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
1
u/XJenso Aug 31 '25
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.
0
u/averagecrazyliberal Aug 31 '25
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.
12
u/pexea12 Aug 31 '25
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.
-2
Sep 01 '25
Python is more Type Safe than TS because Python is actually statically typed unlike JS wich dissolves TSs guarantees at runtime.
5
u/Dillweed999 Aug 31 '25
Check out Robust Python by Patrick Viafore