r/ProgrammingLanguages • u/BeamMeUpBiscotti • 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/
View all comments
Show parent comment
3
u/BeamMeUpBiscotti Feb 25 '26
It's not specific to containers.
The same first-use-pinning mechanism can be used to infer unknown type arguments for any generic type. Pyrefly does that, but Mypy only uses it for empty container literals.
I wouldn't say that it's necessarily "unions are bad", just that 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.
In lieu of a union between two classes there's also the option to infer a common parent class, which is less precise but might be closer to the user's intent in some situations.
Re: un-annotated variables or attributes assigned to different types in different branches, the same tradeoffs apply but type checkers don't necessarily make the same choice here as they do for empty container inference.
ATM, Pyrefly takes the union of the branches for local variables, but uses the first assignment for attributes. The latter is subject to change, since it doesn't allow initializing an attribute to different types in different branches of the constructor.