Skip to content

Commit e1a36bc

Browse files
Pigbibicodex
andcommitted
fix: preserve legacy official event export
Co-Authored-By: Codex <noreply@openai.com>
1 parent 9745240 commit e1a36bc

2 files changed

Lines changed: 64 additions & 19 deletions

File tree

src/political_event_tracking_research/official_event_import.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@
3838
{"issuer", "direct_beneficiary", "industry_context", "unverified"}
3939
)
4040

41+
LEGACY_EVENT_COLUMNS = [
42+
"event_id",
43+
"event_date",
44+
"symbol",
45+
"event_type",
46+
"direction",
47+
"confidence",
48+
"source_url",
49+
"notes",
50+
]
51+
ENTITY_EVENT_COLUMNS = LEGACY_EVENT_COLUMNS + [
52+
"entity_match_type",
53+
"match_evidence",
54+
"relationship_type",
55+
]
56+
4157

4258
@dataclass(frozen=True)
4359
class OfficialRecord:
@@ -148,24 +164,17 @@ def normalize_records(records: list[OfficialRecord]) -> list[dict[str, str]]:
148164
return rows
149165

150166

151-
def import_official_events(input_path: str | Path, output_path: str | Path) -> list[dict[str, str]]:
167+
def import_official_events(
168+
input_path: str | Path,
169+
output_path: str | Path,
170+
*,
171+
include_entity_metadata: bool = False,
172+
) -> list[dict[str, str]]:
152173
records = load_official_records(input_path)
153174
rows = normalize_records(records)
154175
write_csv_rows(
155176
output_path,
156-
[
157-
"event_id",
158-
"event_date",
159-
"symbol",
160-
"event_type",
161-
"direction",
162-
"confidence",
163-
"source_url",
164-
"notes",
165-
"entity_match_type",
166-
"match_evidence",
167-
"relationship_type",
168-
],
177+
ENTITY_EVENT_COLUMNS if include_entity_metadata else LEGACY_EVENT_COLUMNS,
169178
rows,
170179
)
171180
return rows
@@ -177,12 +186,17 @@ def build_arg_parser() -> argparse.ArgumentParser:
177186
)
178187
parser.add_argument("--input", required=True, help="Official-source record CSV.")
179188
parser.add_argument("--output", required=True, help="Output normalized event CSV.")
189+
parser.add_argument(
190+
"--include-entity-metadata",
191+
action="store_true",
192+
help="Opt in to entity metadata columns in the normalized CSV.",
193+
)
180194
return parser
181195

182196

183197
def main(argv: list[str] | None = None) -> None:
184198
args = build_arg_parser().parse_args(argv)
185-
import_official_events(args.input, args.output)
199+
import_official_events(args.input, args.output, include_entity_metadata=args.include_entity_metadata)
186200

187201

188202
if __name__ == "__main__":

tests/test_official_event_import.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_import_official_events_normalizes_to_event_schema(tmp_path: Path) -> No
3131
assert rows[-1]["confidence"] == "low"
3232

3333

34-
def test_legacy_input_defaults_entity_schema_fields_and_serializes_stably(tmp_path: Path) -> None:
34+
def test_legacy_input_defaults_to_legacy_eight_column_serialization(tmp_path: Path) -> None:
3535
output = tmp_path / "events.csv"
3636

3737
rows = import_official_events(ROOT / "examples/official_records.example.csv", output)
@@ -49,12 +49,43 @@ def test_legacy_input_defaults_entity_schema_fields_and_serializes_stably(tmp_pa
4949
"confidence",
5050
"source_url",
5151
"notes",
52-
"entity_match_type",
53-
"match_evidence",
54-
"relationship_type",
5552
]
5653

5754

55+
def test_entity_metadata_serialization_requires_explicit_opt_in(tmp_path: Path) -> None:
56+
input_path = tmp_path / "official-records.csv"
57+
output = tmp_path / "events.csv"
58+
input_path.write_text(
59+
"record_id,record_date,symbol,source_type,event_type,direction,source_url,summary,"
60+
"entity_match_type,match_evidence,relationship_type\n"
61+
"entity-record,2026-01-10,EVT,government_filing,disclosure_buy,bullish,"
62+
"https://www.sec.gov/example/entity-record,Entity evidence.,direct_beneficiary,"
63+
"Named beneficiary in filing.,supplier\n",
64+
encoding="utf-8",
65+
)
66+
67+
import_official_events(input_path, output, include_entity_metadata=True)
68+
69+
with output.open(newline="", encoding="utf-8") as handle:
70+
rows = list(csv.DictReader(handle))
71+
assert list(rows[0]) == [
72+
"event_id",
73+
"event_date",
74+
"symbol",
75+
"event_type",
76+
"direction",
77+
"confidence",
78+
"source_url",
79+
"notes",
80+
"entity_match_type",
81+
"match_evidence",
82+
"relationship_type",
83+
]
84+
assert rows[0]["entity_match_type"] == "direct_beneficiary"
85+
assert rows[0]["match_evidence"] == "Named beneficiary in filing."
86+
assert rows[0]["relationship_type"] == "supplier"
87+
88+
5889
def test_entity_schema_fields_are_imported_and_normalized() -> None:
5990
record = OfficialRecord(
6091
record_id="entity-record",

0 commit comments

Comments
 (0)