VarLookupDict should implement __iter__, causes CPython crash when using statsmodels in marimo notebook
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 990
- Forks
- 106
- Avg merge
- 7d 34m
- Merged PRs (30d)
- 1
Description
Using statsmodels with patsy formulas in a marimo notebook causes a CPython crash (!) if a variable in the patsy formula is undefined. There is an underlying CPython bug (it shouldn't crash!), which I have described at https://github.com/python/cpython/issues/129605#issuecomment-4583354945. This is easily fixable on the patsy side, though. (Happy to submit a small PR if this sounds good to you.)
Here's the real-world reproducer (and how I came across this bug). Create a marimo (0.23.8) notebook with the following cell (that imports statsmodel 0.14.6, which in turn imports patsy 1.0.2).
import polars as pl
import statsmodels.formula.api as smf
df = pl.DataFrame({"a2": range(10), "b": range(10, 20)})
model = smf.ols(
"a ~ b",
data=df.to_pandas(),
)
The CPython process running the marimo kernel will crash when the cell is run. A misspelled column name in a formula string triggers a NameError inside patsy's eval() call (where f_locals is a live VarLookupDict). Marimo calls traceback.print_exception directly from Python to format cell errors — unlike the REPL, which uses C-level PyErr_Display and is robust to this — so the KeyError(0) propagates uncaught and kills the notebook kernel.
Here's the minimal reproducer describing the easy fix on the patsy side:
from patsy.eval import VarLookupDict
d = VarLookupDict([{"x": 1, "y": 2}])
list(d)
# Expected: ["x", "y"]
# Actual: KeyError: 0
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at patsy.eval.VarLookupDict and reproduce the issue with the minimal list(d) example, then check how the object is used as f_locals during eval(). Done means VarLookupDict can be iterated to return its variable names without KeyError, and the misspelled-column case no longer crashes the marimo kernel.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 72/100