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/pa-ccw-social-security-once.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Count Social Security disability and survivor benefits once in Pennsylvania Child Care Works countable income.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ values:
- employment_income
- self_employment_income
- sstb_self_employment_income
# Part I.E; social_security adds the retirement, disability,
# survivors and dependents benefits.
- social_security
- social_security_disability
- social_security_survivors
- ssi
- pension_income
- military_retirement_income
Expand All @@ -27,5 +27,5 @@ metadata:
period: year
label: Pennsylvania CCW countable income sources
reference:
- title: 55 Pa. Code Chapter 3042 Appendix A Part I
- title: "55 Pa. Code Chapter 3042 Appendix A Part I #page 59-60"
href: https://www.pacodeandbulletin.gov/secure/pacode/data/055/chapter3042/055_3042.pdf#page=59
128 changes: 128 additions & 0 deletions policyengine_us/tests/code_health/test_social_security_source_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""Source lists name social_security or its components, not both.

`social_security` adds `social_security_dependents`, `_disability`,
`_retirement` and `_survivors`. A source list that names the total and one of
its components counts that component twice. Pennsylvania Child Care Works
listed `social_security`, `social_security_disability` and
`social_security_survivors`, so SSDI and survivor benefits counted twice
(#10408). Several lists name the components on their own, so adding
`- social_security` to one of them would bring the double count back with no
test failing.

Checked: each value block of a parameter YAML file (the entries under one
date key). A block fails when it has an uncommented entry for the total and
one for a component, as block list entries (`- social_security`) or in a
flow-style list. Comments are ignored, so a comment that names a component
does not count. A file may switch from the components to the total in a
later block.

Python readers are not checked: the ones that read both subtract a component
from the total (for example, to exclude SSDI) and do not add it twice.

The check reads text without importing the model.
"""

import re
from pathlib import Path


PACKAGE = Path(__file__).resolve().parents[2]
PARAMETERS = PACKAGE / "parameters"
TOTAL = "social_security"
COMPONENTS = [
f"{TOTAL}_{kind}"
for kind in ["dependents", "disability", "retirement", "survivors"]
]
DATE_KEY = re.compile(r"^\s*\d{4}-\d{2}-\d{2}\s*:")


def list_entry_pattern(names: list[str]) -> re.Pattern:
"""A block list entry (`- name`) or a flow list entry (`[a, name]`)."""
alternatives = "|".join(re.escape(name) for name in names)
return re.compile(r"(?:^\s*-\s*|[\[,]\s*)(?:" + alternatives + r")\s*(?:$|[\],])")


TOTAL_ENTRY = list_entry_pattern([TOTAL])
COMPONENT_ENTRY = list_entry_pattern(COMPONENTS)


def double_count_lines(text: str) -> list[int]:
"""Lines of total and component entries that share a value block."""
flagged = set()
total_lines = []
component_lines = []

def close_block():
if total_lines and component_lines:
flagged.update(total_lines + component_lines)
total_lines.clear()
component_lines.clear()

for number, line in enumerate(text.splitlines(), start=1):
code = line.split("#", 1)[0]
if DATE_KEY.search(code):
close_block()
if TOTAL_ENTRY.search(code):
total_lines.append(number)
if COMPONENT_ENTRY.search(code):
component_lines.append(number)
close_block()
return sorted(flagged)


def test_detector_flags_total_with_component_and_skips_everything_else():
assert double_count_lines(
"values:\n"
" 2023-01-01:\n"
" - social_security\n"
" - social_security_disability\n"
" - social_security_survivors # trailing comment\n"
" 2024-01-01: [ssi, social_security, social_security_retirement]\n"
) == [3, 4, 5, 6]
assert (
double_count_lines(
"values:\n"
" 2023-01-01:\n"
" # social_security adds the disability and survivors benefits.\n"
" - social_security\n"
" # - social_security_disability\n"
" - ssi\n"
" 2024-01-01:\n"
" - social_security_disability\n"
" - social_security_survivors\n"
)
== []
)
assert (
double_count_lines(
"values:\n"
" 2023-01-01: [social_security_disability, social_security_dependents]\n"
" 2024-01-01: [social_security, ssi]\n"
)
== []
)
assert (
double_count_lines(
"values:\n"
" 2023-01-01:\n"
" - social_security\n"
" - taxable_social_security\n"
" - social_security_disability_insurance_premiums\n"
)
== []
)


def test_source_lists_do_not_name_social_security_and_a_component():
violations = []
for path in sorted(PARAMETERS.rglob("*.yaml")):
text = path.read_text(encoding="utf-8")
if TOTAL not in text:
continue
for line in double_count_lines(text):
violations.append(f"{path.relative_to(PACKAGE).as_posix()}:{line}")
assert not violations, (
"social_security already adds the dependents, disability, retirement "
"and survivors benefits. List the total or the components, not both, "
"so each benefit counts once. Entries:\n" + "\n".join(violations)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# 55 Pa. Code Appendix A Part I
#
# Sources include: employment_income, self_employment_income,
# sstb_self_employment_income, social_security, social_security_disability,
# social_security_survivors, ssi, pension_income, military_retirement_income,
# total_unemployment_compensation, workers_compensation, veterans_benefits,
# child_support_received, alimony_income, dividend_income, interest_income,
# rental_income
# sstb_self_employment_income, social_security, ssi, pension_income,
# military_retirement_income, total_unemployment_compensation,
# workers_compensation, veterans_benefits, child_support_received,
# alimony_income, dividend_income, interest_income, rental_income
#
# social_security adds the retirement, disability, survivors and dependents
# benefits, so each Social Security benefit counts once (Part I.E).

- name: Case 1, employment income only.
period: 2025-01
Expand Down Expand Up @@ -90,3 +92,72 @@
output:
# Monthly: (30,000 + 18,000) / 12 = 48,000 / 12 = 4,000
pa_ccw_countable_income: 4_000

- name: Case 5, Social Security survivor benefits count once.
period: 2025-01
absolute_error_margin: 0.01
input:
people:
person1:
age: 30
social_security_survivors: 12_000
spm_units:
spm_unit:
members: [person1]
households:
household:
members: [person1]
state_code: PA
output:
# Part I.E: survivors' benefits paid by SSA.
# Monthly: 12,000 / 12 = 1,000
pa_ccw_countable_income: 1_000

- name: Case 6, Social Security disability benefits count once.
period: 2025-01
absolute_error_margin: 0.01
input:
people:
person1:
age: 45
social_security_disability: 9_600
spm_units:
spm_unit:
members: [person1]
households:
household:
members: [person1]
state_code: PA
output:
# Part I.E: disability insurance payments made by SSA.
# Monthly: 9,600 / 12 = 800
pa_ccw_countable_income: 800

- name: Case 7, each Social Security benefit type counts once.
period: 2025-01
absolute_error_margin: 0.01
input:
people:
person1:
age: 45
social_security_disability: 9_600
person2:
age: 8
social_security_dependents: 2_400
person3:
age: 6
social_security_survivors: 3_600
person4:
age: 68
social_security_retirement: 18_000
spm_units:
spm_unit:
members: [person1, person2, person3, person4]
households:
household:
members: [person1, person2, person3, person4]
state_code: PA
output:
# The retirement benefit exceeds the federal SSI benefit rate, so ssi is 0.
# Monthly: (9,600 + 2,400 + 3,600 + 18,000) / 12 = 33,600 / 12 = 2,800
pa_ccw_countable_income: 2_800
Loading