Conversation

I'm finding myself bitching about Python a lot (go figure, it's the main language at $dayjob)

but most of my complaints come down to types. I prefer static types checked by the compiler. I will always prefer this. Compilers will catch things I don't think of all the time.

Python's type annotations are good, but the time it takes mypy to check a large codebase is prohibitive

4
0
0

@robdaemon I’ve come to this conclusion too in my Java/Python $dayJob universe — that no matter how exhaustive I am with the type annotations, it doesn’t *replace* the instant unambiguous feedback loop of static types and I just prefer the latter

1
0
0

@taber the amount of time I lose because I write a type annotation, push up to CI, and 15 minutes later mypy says "nope you put Foo but I see Foo | str | None"

gaaaaaaaaah

1
0
0

@taber that feedback on what mypy sees in reality is super useful but the time it takes to give that to me kills

1
0
0

@robdaemon yeah exactly this — the process makes me wish I didn’t bother and just accepted all kinds of really boring subtle type mistakes!

1
0
0

@taber I work on a large mono-repo and this is just a snippet of what I'm waiting on from CI to tell me what I've done wrong in this refactor

0
0
0

@robdaemon wrong data is wrong data, doesn't matter what the type is. 🤪

1
0
0

@fozztexx hahahaha yeah it's definitely not as good as dependent types and doesn't eliminate validation

but it certainly helps!

0
0
0

@robdaemon Right now I'm relying more on dynamic type checks (thanks, attrs validators), because I feel that the static type history in Python is hopeless. Checking data types during __init__ prevents 80% of type issues from propagating, IMO.

1
0
0

@bkim I hadn't seen attrs, I'll give that a look!

the codebase I'm working on is older, and has slowly been adding type annotations over time. The hardest part is the linter "you modified this function but didn't add types" for the legacy portions

1
0
0

@robdaemon Not sure if you’re already aware of these or not, but dmypy (https://mypy.readthedocs.io/en/latest/mypy_daemon.html) and pyright are things that people here use to mitigate mypy’s slowness.

1
0
1

@alpha yeah we use dmypy here, it works pretty well for local dev (still slightly laggy, and I have to scope it to specific folders) but on CI runs, dmypy isn't helpful

0
0
1

@robdaemon @bkim Also pydantic is a common tool used for validating external data into types. People here have started adopting it, thankfully.

2
0
1

@alpha @bkim yep we're using Pydantic on DTOs around here now too

0
0
1

@alpha @robdaemon attrs is more like dataclasses (but actually predates it while allowing validation!), and it has a companion lib called cattrs that is similar to pydantic.

https://attrs.org/en/stable/why.html#pydantic

1
0
0

@bkim @robdaemon Yeah, it’s somewhat unfortunate that dataclasses is in stdlib now since it makes attrs a harder sell to add as a dependency. As such, I’ve never had the opportunity to actually compare pydantic against cattrs.

0
0
0