2026-04-28

Stop doing Container[Any]

Given a container like:

@dataclass
class Container(Generic[T]):
    value: T

It's tempting to type functions that could take any old Container like:

def f(x: Container[Any]) -> None:
    ...

However, if you do that, the following won't raise a type error:

def f(x: Container[Any]) -> None:
    x.value + "some-string"

Any has snuck into your type system.

Instead, use object, then you'll get an error:

def f(x: Container[object]) -> None:
    x.value + "some-string"  # error: Unsupported left operand type for + ("object")  [operator]

If you don't know what type a value is, let the typechecker know you don't know.

Maybe you never cared what type it was, maybe you need to if not isinstance(x.value, SomeType): raise ...