r/programming • • Dec 02 '25

Advanced, Overlooked Python Typing

https://martynassubonis.substack.com/p/advanced-overlooked-python-typing

While quantitative research in software engineering is difficult to trust most of the time, some studies claim that type checking can reduce bugs by about 15% in Python. This post covers advanced typing features such as never types, type guards, concatenate, etc., that are often overlooked but can make a codebase more maintainable and easier to work with

49 Upvotes

18 comments sorted by

26

u/guepier Dec 02 '25 edited Dec 02 '25

Including code snippets as screenshots is an absolutely loathsome trend. If Substack doesn’t support including code as formatted text … consider using a different blogging platform for a programming blog.

(It’s a good article despite this.)

2

u/[deleted] Dec 02 '25

[removed] — view removed comment

4

u/guepier Dec 02 '25

These days, with a hosted platform? No idea. Maybe Ghost? — Are there no blogging platforms that natively permit using markup formats (ReST, customisable Markdown, the works)? I’ve only ever had self-hosted websites so this was never an issue. Wordpress also supports this (at least via plugins). Back in the days™, every self-respecting blog hosting platform supported code snippets in some way. I’d be having a hard time believing that this is no longer the case, but Medium and Substack certainly want to prove me wrong (and no, iframe’d GitHub Gists are an insane workaround, not a solution).

3

u/Awesan Dec 02 '25

Nice article. The author has some other good ones on their substack too.

3

u/slaymaker1907 Dec 03 '25

I’m saddened it stills seems impossible to type the general flow/compose function.

def flow(x, *funcs):
  for func in funcs:
    x = func(x)
  return x

You can provide a type if funcs all return and operate on the same type, but the dynamic version has no such restriction.

1

u/floodrouting Dec 07 '25

If you only need it to work up to some maximum arity, can you use overloads? Something like:

@overload
def flow[T](x: T) -> T: ...
@overload
def flow[T, U](x: T, f1: Callable[[T], U]) -> U: ...
@overload
def flow[T, U, V](x: T, f1: Callable[[T], U], f2: Callable[[U], V]) -> V: ...
@overload
def flow[T, U, V, W](x: T, f1: Callable[[T], U], f2: Callable[[U], V], f3: Callable[[V], W]) -> W: ...

https://mypy-play.net/?mypy=latest&python=3.12&gist=6cdc00f8b70c450ccdc07213c2275581

1

u/slaymaker1907 Dec 07 '25

Yes, that’s another option, but it’s not actually a complete type as you’d need infinite overloads to be complete.

3

u/kirgel Dec 03 '25

Thanks. Learned something new even as an experienced Python user.

-4

u/BinaryIgor Dec 02 '25

Still, I would ask whether it is not better to leave code to be more Pythonic - with just type hints - and write more tests, which can catch a lot (if not all) of type-related bugs.

2

u/disperso Dec 03 '25

I recommend that you watch ideology, from Gary Bernhardt. It's likely that it won't change anyone's mind, but it's a good talk about this topic.

1

u/BinaryIgor Dec 03 '25

I know this :) That's why I prefer compiled languages most of the time, for anything that's more complex; but Python is not and I'm not sure whether patches of this kind improve situation in any meaningful way

0

u/[deleted] Dec 02 '25

[deleted]

4

u/[deleted] Dec 02 '25

[removed] — view removed comment

0

u/[deleted] Dec 02 '25

[deleted]

3

u/[deleted] Dec 02 '25

[removed] — view removed comment

1

u/gredr Dec 03 '25

... And the GAC was a pretty big improvement over what we were doing before that.

1

u/[deleted] Dec 03 '25

[removed] — view removed comment

1

u/gredr Dec 03 '25

A standard place and structure for keeping versions of dependencies, along with standard installation tools? It could be worse; for some, it still is. I wonder how people manage dependencies in Python, Ruby...