Why Python Type System Sucks

An exploration of the limitations and challenges of Python's type system.

Unpredictable Type Inference

  • Even with all possible type hints, Python type system don't allow passing TypedDict value to param.
from collections.abc import Mapping, MutableMapping
from typing import TypedDict

def my_func(v: Mapping[str, str] | dict[str, str] | MutableMapping[str, str]):  ...

class MyTypedDict(TypedDict): ...

val: MyTypedDict = {}
my_func(val) # 🚨 ERROR: "MyTypedDict" is not assignable

Nested Types are hard

  • Python type system is not good at handling nested types. We've to define a new type for each nested type.
from typing import Any, TypedDict

class ContextNode(TypedDict):
    output: Any

class Context(TypedDict):
    nodes: ContextNode

In typescript this is as simple as:

interface Context {
  nodes: {
    output: any
  }
}

Beware with Sequence

from collections.abc import Sequence

def my_func(errors: Sequence[str]): ...

my_func(["Some Error"])
my_func("Some Error") # Valid, Because strings are sequence as well