Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4275.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A household calculation (`POST /<country_id>/calculate` or `/calculate-full`) now answers 400, listing each problem, for a `policy` the calculation can't read, instead of a 500 after several seconds: a policy that isn't an object, a parameter not set to an object of period keys, a period key neither the US nor the UK model reads, or a value its parameter can't take, such as `null`, a list or non-numeric text for a number.
23 changes: 11 additions & 12 deletions policyengine_api/country.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Loaded country tax-benefit systems, their metadata and household calculation."""

import contextlib
import importlib
import inspect
import json
Expand Down Expand Up @@ -48,6 +47,10 @@
)
from policyengine_api.utils import get_safe_json
from policyengine_api.utils.get_current_law import get_current_law_policy_id
from policyengine_api.utils.household_policy_validation import (
conversion_type,
convert_reform_value,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -589,17 +592,13 @@ def _normalize_reform_values(
normalized_reform = {}
for parameter_name, parameter_updates in reform.items():
parameter = get_parameter(system.parameters, parameter_name)
normalized_reform[parameter_name] = {}
for time_period, value in parameter_updates.items():
node_type = type(parameter.values_list[-1].value)
if node_type is int:
node_type = float
numeric_value = value
with contextlib.suppress(TypeError, ValueError, OverflowError):
numeric_value = float(value)
normalized_reform[parameter_name][time_period] = node_type(
numeric_value
)
# The conversion find_unreadable_policy_inputs tries before a
# calculation request is accepted.
value_type = conversion_type(parameter.values_list[-1].value)
normalized_reform[parameter_name] = {
time_period: convert_reform_value(value, value_type)
for time_period, value in parameter_updates.items()
}
return normalized_reform

def _apply_reform_to_system(
Expand Down
26 changes: 18 additions & 8 deletions policyengine_api/services/household_calculation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
validate_spm_calculation_provenance,
)
from policyengine_api.utils.deprecated_inputs import drop_deprecated_inputs
from policyengine_api.utils.household_policy_validation import (
find_unreadable_policy_inputs,
)
from policyengine_api.utils.input_validation import find_invalid_inputs

if TYPE_CHECKING:
Expand Down Expand Up @@ -97,8 +100,9 @@ class HouseholdNotFoundError(LookupError):
class InvalidHouseholdInputsError(ValueError):
"""Raised when a household or policy has inputs the country model cannot use.

These are names the country model does not know and axes policyengine-core
cannot expand.
These are names the country model does not know, axes policyengine-core
cannot expand and, for a calculation request, parts of the policy the
calculation cannot read.
"""

def __init__(self, invalid_inputs: list[Any]) -> None:
Expand Down Expand Up @@ -426,12 +430,18 @@ def prepare_household_calculation(
deprecated_inputs = drop_deprecated_inputs(household_json)
household_json = deprecated_inputs.household
# The axes are checked as policyengine-core receives them, after the
# deprecated ones are dropped.
invalid_inputs = find_invalid_inputs(
household_json,
policy_json,
country.metadata,
)
# deprecated ones are dropped. The policy's shape, period keys and
# values are checked here, before parsing copies the model's
# tax-benefit system; a stored policy, which the caller didn't send,
# isn't (calculate_stored_household).
invalid_inputs = [
*find_invalid_inputs(
household_json,
policy_json,
country.metadata,
),
*find_unreadable_policy_inputs(policy_json, country.metadata, country_id),
]
if invalid_inputs:
raise InvalidHouseholdInputsError(invalid_inputs)

Expand Down
Loading
Loading