Skip to content

Preserve required variables of stateful transforms - #285

Open
ktz03 wants to merge 1 commit into
matthewwardrop:mainfrom
ktz03:fix/required-variables-stateful
Open

ktz03 wants to merge 1 commit into
matthewwardrop:mainfrom
ktz03:fix/required-variables-stateful

Conversation

@ktz03

@ktz03 ktz03 commented Oct 11, 2026

Copy link
Copy Markdown

Fixes #284.

Formula("center(x)").required_variables returns an empty set instead of {"x"}, even though generating its model matrix uses x. The default no-op variable getter still evaluates the transform call's arguments during introspection. Without data, this raises NameError, which Factor.required_variables and Token.required_variables suppress.

Keep absent variable getters as None, and delegate extraction only when a transform has an explicit getter. Ordinary stateful transforms then use the existing AST traversal without evaluating their arguments. Explicit getters, including Q() for quoted column names, remain active.

Validation on Windows with Python 3.12.14:

  • Full configured pytest command with coverage: 657 passed, 27 skipped. The baseline has the same 27 skips and 9 warnings.
  • Added 14 regression and control cases: 10 fail and 4 pass on unchanged main; all 14 pass with the fix.
  • Official pinned Ruff 0.15.15 check and format checks pass; Mypy 2.1.0 passes for all 58 source files.

No performance benchmark or other Python-version run was performed locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formulaic.Formula.required_variables omits stateful transforms in version 1.2.2

1 participant