|
| 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 | +import copy |
| 12 | +from dataclasses import dataclass |
| 13 | + |
| 14 | + |
| 15 | +# Registry of removed/renamed model variables that legacy partner traffic |
| 16 | +# may still pass. The value is a one-line migration hint surfaced verbatim |
| 17 | +# in the warning message - keep it actionable. |
| 18 | +DEPRECATED_VARIABLES: dict[str, str] = { |
| 19 | + "medical_out_of_pocket_expenses": ( |
| 20 | + "Removed in policyengine-us 1.673.0. Migrate non-premium spending " |
| 21 | + "to `other_medical_expenses` and premium spending to " |
| 22 | + "`health_insurance_premiums`." |
| 23 | + ), |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +@dataclass(frozen=True) |
| 28 | +class DeprecatedVariableWarning: |
| 29 | + """A removed/renamed variable was supplied; dropped before the engine ran.""" |
| 30 | + |
| 31 | + variable: str |
| 32 | + entity_plural: str |
| 33 | + entity_id: str |
| 34 | + hint: str |
| 35 | + |
| 36 | + @property |
| 37 | + def message(self) -> str: |
| 38 | + location = f"`{self.entity_plural}/{self.entity_id}`" |
| 39 | + if self.entity_plural == "axes": |
| 40 | + location = f"`axes[{self.entity_id}].name`" |
| 41 | + return ( |
| 42 | + f"Input `{self.variable}` on {location} is deprecated and was " |
| 43 | + f"ignored for this calculation. {self.hint}" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@dataclass(frozen=True) |
| 48 | +class DeprecatedInputsResult: |
| 49 | + """A household copy with deprecated inputs removed plus warnings.""" |
| 50 | + |
| 51 | + household: dict |
| 52 | + warnings: list[DeprecatedVariableWarning] |
| 53 | + |
| 54 | + |
| 55 | +def drop_deprecated_inputs( |
| 56 | + household: dict, |
| 57 | +) -> DeprecatedInputsResult: |
| 58 | + """Return a household copy with deprecated input keys stripped. |
| 59 | +
|
| 60 | + Returns one warning per (entity, deprecated-key) occurrence. The |
| 61 | + caller's ``household`` is never mutated; downstream simulation |
| 62 | + receives the returned copy. |
| 63 | +
|
| 64 | + Non-dict inputs are returned unchanged with no warnings; downstream |
| 65 | + code retains its existing bad-shape behavior. |
| 66 | + """ |
| 67 | + warnings: list[DeprecatedVariableWarning] = [] |
| 68 | + |
| 69 | + if not isinstance(household, dict): |
| 70 | + return DeprecatedInputsResult(household=household, warnings=warnings) |
| 71 | + |
| 72 | + cleaned_household = copy.deepcopy(household) |
| 73 | + |
| 74 | + for entity_plural, entity_group in cleaned_household.items(): |
| 75 | + if entity_plural == "axes": |
| 76 | + continue |
| 77 | + if not isinstance(entity_group, dict): |
| 78 | + continue |
| 79 | + for entity_id, variables in entity_group.items(): |
| 80 | + if not isinstance(variables, dict): |
| 81 | + continue |
| 82 | + for variable_name in list(variables.keys()): |
| 83 | + hint = DEPRECATED_VARIABLES.get(variable_name) |
| 84 | + if hint is None: |
| 85 | + continue |
| 86 | + warnings.append( |
| 87 | + DeprecatedVariableWarning( |
| 88 | + variable=variable_name, |
| 89 | + entity_plural=entity_plural, |
| 90 | + entity_id=entity_id, |
| 91 | + hint=hint, |
| 92 | + ) |
| 93 | + ) |
| 94 | + del variables[variable_name] |
| 95 | + |
| 96 | + _drop_deprecated_axes(cleaned_household, warnings) |
| 97 | + |
| 98 | + return DeprecatedInputsResult(household=cleaned_household, warnings=warnings) |
| 99 | + |
| 100 | + |
| 101 | +def _drop_deprecated_axes( |
| 102 | + household: dict, warnings: list[DeprecatedVariableWarning] |
| 103 | +) -> None: |
| 104 | + axes = household.get("axes") |
| 105 | + if not isinstance(axes, list): |
| 106 | + return |
| 107 | + |
| 108 | + changed = False |
| 109 | + retained_entries = [] |
| 110 | + |
| 111 | + for entry_index, entry in enumerate(axes): |
| 112 | + if isinstance(entry, list): |
| 113 | + retained_axes = [] |
| 114 | + for axis_index, axis in enumerate(entry): |
| 115 | + location = f"{entry_index}][{axis_index}" |
| 116 | + if _is_deprecated_axis(axis, location, warnings): |
| 117 | + changed = True |
| 118 | + continue |
| 119 | + retained_axes.append(axis) |
| 120 | + if retained_axes: |
| 121 | + retained_entries.append(retained_axes) |
| 122 | + continue |
| 123 | + |
| 124 | + location = str(entry_index) |
| 125 | + if _is_deprecated_axis(entry, location, warnings): |
| 126 | + changed = True |
| 127 | + continue |
| 128 | + retained_entries.append(entry) |
| 129 | + |
| 130 | + if not changed: |
| 131 | + return |
| 132 | + if retained_entries: |
| 133 | + household["axes"] = retained_entries |
| 134 | + else: |
| 135 | + del household["axes"] |
| 136 | + |
| 137 | + |
| 138 | +def _is_deprecated_axis( |
| 139 | + axis, location: str, warnings: list[DeprecatedVariableWarning] |
| 140 | +) -> bool: |
| 141 | + if not isinstance(axis, dict): |
| 142 | + return False |
| 143 | + |
| 144 | + variable_name = axis.get("name") |
| 145 | + hint = DEPRECATED_VARIABLES.get(variable_name) |
| 146 | + if hint is None: |
| 147 | + return False |
| 148 | + |
| 149 | + warnings.append( |
| 150 | + DeprecatedVariableWarning( |
| 151 | + variable=variable_name, |
| 152 | + entity_plural="axes", |
| 153 | + entity_id=location, |
| 154 | + hint=hint, |
| 155 | + ) |
| 156 | + ) |
| 157 | + return True |
0 commit comments