Skip to content

Add default_value and value_type to Variable class #226

@anth-volk

Description

@anth-volk

Summary

Add default_value and value_type fields to the Variable class in policyengine.py to expose these properties from policyengine-core.

Motivation

The policyengine-api-v2-alpha and policyengine-app-v2 need access to variable default values to properly initialize variables when users add them to household builders. Currently, the Variable class in policyengine.py only exposes:

  • id, name, entity, description, data_type, possible_values

But policyengine-core's Variable class also has:

  • default_value - the default value for the variable (e.g., 0 for floats, False for bools, "SINGLE" for enums)
  • value_type - the Python type of the variable (float, bool, int, str, Enum, datetime.date)

Without these fields, downstream tools must load the entire country-specific CountryTaxBenefitSystem just to access default values, which defeats the purpose of policyengine.py as a consistent abstraction layer.

Changes Required

  1. src/policyengine/core/variable.py: Add fields to Variable class:

    default_value: Any = None
    value_type: type | None = None
  2. src/policyengine/tax_benefit_models/us/model.py and uk/model.py: Extract and serialize values:

    default_val = var_obj.default_value
    if var_obj.value_type is Enum:
        default_val = default_val.name
    elif var_obj.value_type is datetime.date:
        default_val = default_val.isoformat()
    
    variable = Variable(
        # ... existing fields ...
        default_value=default_val,
        value_type=var_obj.value_type,
    )

Supported value_types (from policyengine-core config)

value_type default
bool False
int 0
float 0
str ""
Enum enum member name (string)
datetime.date "1970-01-01" (ISO format)

Branch

Implementation available on branch: feat/add-variable-default-value

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions