From b711aa56b7ebb75490830fe38f11f92ee8a90bd1 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Thu, 23 Jul 2026 19:50:36 -0400 Subject: [PATCH] Guard remaining MD local tax variables against unknown counties md_applicable_local_tax_rate and md_flat_rate_county_tax carried the same unguarded flat_rate[county] indexing that #9116 fixed in md_withheld_income_tax: an MD household whose county resolves to County.UNKNOWN (the default when county_fips is unmapped or absent) raised ParameterNotFoundError for gov.local.md.flat_rate.UNKNOWN, crashing any computation of MD local income tax. Extend the same UNKNOWN fallback to the default MD county to both variables. Extends #9116; same crash class as #8975. Co-Authored-By: Claude Fable 5 --- changelog.d/md-local-unknown-county.fixed.md | 1 + .../local/md_applicable_local_tax_rate.yaml | 20 ++++++++++++++++ .../income/local/md_flat_rate_county_tax.yaml | 23 +++++++++++++++++++ .../local/md_applicable_local_tax_rate.py | 6 ++++- .../income/local/md_flat_rate_county_tax.py | 6 ++++- 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 changelog.d/md-local-unknown-county.fixed.md create mode 100644 policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_flat_rate_county_tax.yaml diff --git a/changelog.d/md-local-unknown-county.fixed.md b/changelog.d/md-local-unknown-county.fixed.md new file mode 100644 index 00000000000..0c4116ed241 --- /dev/null +++ b/changelog.d/md-local-unknown-county.fixed.md @@ -0,0 +1 @@ +Fixed crashes in the Maryland local tax variables md_applicable_local_tax_rate and md_flat_rate_county_tax for Maryland households whose county is unknown, by falling back to the same default county used elsewhere. diff --git a/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_applicable_local_tax_rate.yaml b/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_applicable_local_tax_rate.yaml index d54580bf9d6..9a1cee15d62 100644 --- a/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_applicable_local_tax_rate.yaml +++ b/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_applicable_local_tax_rate.yaml @@ -48,3 +48,23 @@ county_str: FREDERICK_COUNTY_MD output: md_applicable_local_tax_rate: 0.0296 + +- name: Allegany County uses its flat rate (2025) + period: 2025 + absolute_error_margin: 0.0001 + input: + state_code_str: MD + county_str: ALLEGANY_COUNTY_MD + output: + md_applicable_local_tax_rate: 0.0303 + +- name: Unknown county in an MD household falls back to Allegany County (issue #8975) + period: 2025 + absolute_error_margin: 0.0001 + input: + state_code_str: MD + county_str: UNKNOWN + output: + # gov.local.md.flat_rate has no UNKNOWN key; the formula falls back to + # ALLEGANY_COUNTY_MD. Before the fix this raised ParameterNotFoundError. + md_applicable_local_tax_rate: 0.0303 diff --git a/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_flat_rate_county_tax.yaml b/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_flat_rate_county_tax.yaml new file mode 100644 index 00000000000..795e980bb3c --- /dev/null +++ b/policyengine_us/tests/policy/baseline/gov/states/md/tax/income/local/md_flat_rate_county_tax.yaml @@ -0,0 +1,23 @@ +- name: Allegany County MD flat rate county tax baseline (2025) + period: 2025 + absolute_error_margin: 0.01 + input: + state_code_str: MD + county_str: ALLEGANY_COUNTY_MD + md_taxable_income: 30_000 + output: + # 3.03% Allegany rate * 30,000 = 909 + md_flat_rate_county_tax: 909 + +- name: Unknown county in an MD household falls back to Allegany County (issue #8975) + period: 2025 + absolute_error_margin: 0.01 + input: + state_code_str: MD + county_str: UNKNOWN + md_taxable_income: 30_000 + output: + # gov.local.md.flat_rate has no UNKNOWN key; the formula falls back to + # ALLEGANY_COUNTY_MD, so this must equal the Allegany baseline above. + # Before the fix this raised ParameterNotFoundError instead of computing. + md_flat_rate_county_tax: 909 diff --git a/policyengine_us/variables/gov/states/md/tax/income/local/md_applicable_local_tax_rate.py b/policyengine_us/variables/gov/states/md/tax/income/local/md_applicable_local_tax_rate.py index ad626d0096a..7db28e18d56 100644 --- a/policyengine_us/variables/gov/states/md/tax/income/local/md_applicable_local_tax_rate.py +++ b/policyengine_us/variables/gov/states/md/tax/income/local/md_applicable_local_tax_rate.py @@ -19,7 +19,11 @@ def formula(tax_unit, period, parameters): # Guard against non-MD counties in microsimulation: # defined_for masks results but doesn't prevent formula execution. in_md = tax_unit.household("state_code_str", period) == "MD" - safe_county = where(in_md, county, "ALLEGANY_COUNTY_MD") + # Fall back to a default MD county when the county is unknown/unmapped + # (County.UNKNOWN is the default), since gov.local.md.flat_rate has no + # UNKNOWN key and would otherwise raise ParameterNotFoundError. The + # non-MD path already falls back to the same default county. + safe_county = where(in_md & (county != "UNKNOWN"), county, "ALLEGANY_COUNTY_MD") p = parameters(period).gov.local.md flat_rate = p.flat_rate[safe_county] diff --git a/policyengine_us/variables/gov/states/md/tax/income/local/md_flat_rate_county_tax.py b/policyengine_us/variables/gov/states/md/tax/income/local/md_flat_rate_county_tax.py index 784c45006fe..447cad93edf 100644 --- a/policyengine_us/variables/gov/states/md/tax/income/local/md_flat_rate_county_tax.py +++ b/policyengine_us/variables/gov/states/md/tax/income/local/md_flat_rate_county_tax.py @@ -17,7 +17,11 @@ def formula(tax_unit, period, parameters): # Guard against non-MD counties in microsimulation # defined_for masks results but doesn't prevent formula execution in_md = tax_unit.household("state_code_str", period) == "MD" - safe_county = where(in_md, county, "ALLEGANY_COUNTY_MD") + # Fall back to a default MD county when the county is unknown/unmapped + # (County.UNKNOWN is the default), since gov.local.md.flat_rate has no + # UNKNOWN key and would otherwise raise ParameterNotFoundError. The + # non-MD path already falls back to the same default county. + safe_county = where(in_md & (county != "UNKNOWN"), county, "ALLEGANY_COUNTY_MD") p = parameters(period).gov.local.md.flat_rate flat_rate = p[safe_county]