r/ProgrammingLanguages • • Feb 25 '26

Blog post Blog: Empty Container Inference Strategies for Python

Empty containers like [] and {} are everywhere in Python. It's super common to see functions start by creating an empty container, filling it up, and then returning the result.

Take this, for example:

def my_func(ys: dict[str, int]):
  x = {}
  for k, v in ys.items():
    if some_condition(k):
      x.setdefault("group0", []).append((k, v))
    else:
      x.setdefault("group1", []).append((k, v))
  return x

This seemingly innocent coding pattern poses an interesting challenge for Python type checkers. Normally, when a type checker sees x = y without a type hint, it can just look at y to figure out x's type. The problem is, when y is an empty container (like x = {} above), the checker knows it's a dict, but has no clue what's going inside.

The big question is: How is the type checker supposed to analyze the rest of the function without knowing x's type?

Different type checkers implement distinct strategies to answer this question. This blog will examine these different approaches, weighing their pros and cons, and which type checkers implement each approach.

Full blog: https://pyrefly.org/blog/container-inference-comparison/

19 Upvotes

13 comments sorted by

View all comments

Show parent comment

4

u/Uncaffeinated 1subml, polysubml, cubiml Feb 26 '26 edited Feb 26 '26

if you do make a mistake where you accidentally create a union, it's fairly hard to debug later since you need to figure out where the union came from.

This seems like an easily solvable problem to me. Just augment the compiler error messages to tell the user. My language already does something similar to this.

Here's an example:

Code:

type Bar = {name: str};
type Baz = {name: str; age: int};
let a: Bar = Bar {name="Abe"};
let b: Baz = Baz {name="Bob"; age=12};

let f = fun x -> x.name;
f a;
f b;

Error message:

TypeError: Incompatible types here:
let b: Baz = Baz {name="Bob"; age=12};
let f = fun x -> x.name;
                ^~~~~~  
f a;
f b;
Note: The value could originate with one type here:
type Bar = {name: str};
type Baz = {name: str; age: int};
let a: Bar = Bar {name="Abe"};
      ^~~                     
let b: Baz = Baz {name="Bob"; age=12};
but it could also be a value with an incompatible type originating here:
type Baz = {name: str; age: int};
let a: Bar = Bar {name="Abe"};
let b: Baz = Baz {name="Bob"; age=12};
      ^~~                             
let f = fun x -> x.name;
Hint: To narrow down the cause of the type mismatch, consider adding an explicit type annotation here:
let b: Baz = Baz {name="Bob"; age=12};
let f = fun (x: _) -> x.name;
            + ++++            
f a;
f b;