|
| 1 | +"""Detect and drop deprecated input variables before they reach the engine. |
| 2 | +
|
| 3 | +Without this, a partner who passes a removed model variable (e.g. |
| 4 | +``medical_out_of_pocket_expenses``, deleted in policyengine-us 1.673.0) |
| 5 | +crashes the simulation with ``VariableNotFoundError`` (HTTP 500). Dropping |
| 6 | +the input and surfacing a structured warning gives partners a soft |
| 7 | +landing — every other output computes normally; only outputs that |
| 8 | +depended on the deprecated input fall back to defaults. |
| 9 | +""" |
| 10 | + |
| 11 | +from dataclasses import dataclass |
| 12 | + |
| 13 | + |
| 14 | +# Registry of removed/renamed model variables that legacy partner traffic |
| 15 | +# may still pass. The value is a one-line migration hint surfaced verbatim |
| 16 | +# in the warning message — keep it actionable. |
| 17 | +DEPRECATED_VARIABLES: dict[str, str] = { |
| 18 | + "medical_out_of_pocket_expenses": ( |
| 19 | + "Removed in policyengine-us 1.673.0. Migrate non-premium spending " |
| 20 | + "to `other_medical_expenses` and premium spending to " |
| 21 | + "`health_insurance_premiums`." |
| 22 | + ), |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +@dataclass(frozen=True) |
| 27 | +class DeprecatedVariableWarning: |
| 28 | + """A removed/renamed variable was supplied; dropped before the engine ran.""" |
| 29 | + |
| 30 | + variable: str |
| 31 | + entity_plural: str |
| 32 | + entity_id: str |
| 33 | + hint: str |
| 34 | + |
| 35 | + @property |
| 36 | + def message(self) -> str: |
| 37 | + location = f"`{self.entity_plural}/{self.entity_id}`" |
| 38 | + if self.entity_plural == "axes": |
| 39 | + location = f"`axes[{self.entity_id}].name`" |
| 40 | + return ( |
| 41 | + f"Input `{self.variable}` on {location} is deprecated and was " |
| 42 | + f"ignored for this calculation. {self.hint}" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def drop_deprecated_inputs( |
| 47 | + household: dict, |
| 48 | +) -> list[DeprecatedVariableWarning]: |
| 49 | + """Strip deprecated input keys from ``household`` in place. |
| 50 | +
|
| 51 | + Returns one warning per (entity, deprecated-key) occurrence. Mutates |
| 52 | + ``household`` so downstream validation and the simulation never see |
| 53 | + the deprecated keys. |
| 54 | +
|
| 55 | + Non-dict inputs are returned unchanged with no warnings; the |
| 56 | + Pydantic schema check that runs immediately after will reject the |
| 57 | + bad shape with a 400. |
| 58 | + """ |
| 59 | + warnings: list[DeprecatedVariableWarning] = [] |
| 60 | + |
| 61 | + if not isinstance(household, dict): |
| 62 | + return warnings |
| 63 | + |
| 64 | + for entity_plural, entity_group in household.items(): |
| 65 | + if entity_plural == "axes": |
| 66 | + continue |
| 67 | + if not isinstance(entity_group, dict): |
| 68 | + continue |
| 69 | + for entity_id, variables in entity_group.items(): |
| 70 | + if not isinstance(variables, dict): |
| 71 | + continue |
| 72 | + for variable_name in list(variables.keys()): |
| 73 | + hint = DEPRECATED_VARIABLES.get(variable_name) |
| 74 | + if hint is None: |
| 75 | + continue |
| 76 | + warnings.append( |
| 77 | + DeprecatedVariableWarning( |
| 78 | + variable=variable_name, |
| 79 | + entity_plural=entity_plural, |
| 80 | + entity_id=entity_id, |
| 81 | + hint=hint, |
| 82 | + ) |
| 83 | + ) |
| 84 | + del variables[variable_name] |
| 85 | + |
| 86 | + _drop_deprecated_axes(household, warnings) |
| 87 | + |
| 88 | + return warnings |
| 89 | + |
| 90 | + |
| 91 | +def _drop_deprecated_axes( |
| 92 | + household: dict, warnings: list[DeprecatedVariableWarning] |
| 93 | +) -> None: |
| 94 | + axes = household.get("axes") |
| 95 | + if not isinstance(axes, list): |
| 96 | + return |
| 97 | + |
| 98 | + changed = False |
| 99 | + retained_entries = [] |
| 100 | + |
| 101 | + for entry_index, entry in enumerate(axes): |
| 102 | + if isinstance(entry, list): |
| 103 | + retained_axes = [] |
| 104 | + for axis_index, axis in enumerate(entry): |
| 105 | + location = f"{entry_index}][{axis_index}" |
| 106 | + if _is_deprecated_axis(axis, location, warnings): |
| 107 | + changed = True |
| 108 | + continue |
| 109 | + retained_axes.append(axis) |
| 110 | + if retained_axes: |
| 111 | + retained_entries.append(retained_axes) |
| 112 | + continue |
| 113 | + |
| 114 | + location = str(entry_index) |
| 115 | + if _is_deprecated_axis(entry, location, warnings): |
| 116 | + changed = True |
| 117 | + continue |
| 118 | + retained_entries.append(entry) |
| 119 | + |
| 120 | + if not changed: |
| 121 | + return |
| 122 | + if retained_entries: |
| 123 | + household["axes"] = retained_entries |
| 124 | + else: |
| 125 | + del household["axes"] |
| 126 | + |
| 127 | + |
| 128 | +def _is_deprecated_axis( |
| 129 | + axis, location: str, warnings: list[DeprecatedVariableWarning] |
| 130 | +) -> bool: |
| 131 | + if not isinstance(axis, dict): |
| 132 | + return False |
| 133 | + |
| 134 | + variable_name = axis.get("name") |
| 135 | + hint = DEPRECATED_VARIABLES.get(variable_name) |
| 136 | + if hint is None: |
| 137 | + return False |
| 138 | + |
| 139 | + warnings.append( |
| 140 | + DeprecatedVariableWarning( |
| 141 | + variable=variable_name, |
| 142 | + entity_plural="axes", |
| 143 | + entity_id=location, |
| 144 | + hint=hint, |
| 145 | + ) |
| 146 | + ) |
| 147 | + return True |
0 commit comments