From ec56f4a4870191c217b7691035b4188aa3cf8294 Mon Sep 17 00:00:00 2001 From: Caleb Sitton Date: Tue, 21 Jul 2026 16:06:27 -0600 Subject: [PATCH] fix: fix bug in sorting cashflow streams --- dcaf/streams/cashflows.py | 4 ++-- tests/unit/test_cashflow_stream.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dcaf/streams/cashflows.py b/dcaf/streams/cashflows.py index a003328..9d069b2 100644 --- a/dcaf/streams/cashflows.py +++ b/dcaf/streams/cashflows.py @@ -645,7 +645,7 @@ def sort( descending: bool = ..., ) -> "CashFlowStream": ... @overload - def sort(self) -> "CashFlowStream": ... + def sort(self, *, descending: bool = ...) -> "CashFlowStream": ... def sort( self, @@ -712,7 +712,7 @@ def sort( if fn is not None: return super().sort(fn, descending=descending) if attr is None: - return super().sort() + return super().sort(attr="date", descending=descending) return super().sort(attr=attr, descending=descending) def scale(self, factor: float) -> "CashFlowStream": diff --git a/tests/unit/test_cashflow_stream.py b/tests/unit/test_cashflow_stream.py index 29af78b..58c4509 100644 --- a/tests/unit/test_cashflow_stream.py +++ b/tests/unit/test_cashflow_stream.py @@ -134,8 +134,7 @@ def test_from_recurring_annual_escalation_is_date_based(): ) expected_dates = [date(2026, 4, 4), date(2026, 5, 4), date(2026, 6, 4)] expected_amounts = [ - -200.0 * _annual_factor(date(2026, 3, 5), flow_date, 0.1) - for flow_date in expected_dates + -200.0 * _annual_factor(date(2026, 3, 5), flow_date, 0.1) for flow_date in expected_dates ] for i, flow in enumerate(cf_stream.entries): assert flow.date == expected_dates[i] @@ -888,6 +887,13 @@ def test_sort_bare_call_sorts_by_date(_create_cf_stream): assert result.entries == [flows[0], flows[1], flows[2], flows[3]] +def test_sort_default_attribute_descending(_create_cf_stream): + """sort(descending=True) sorts by the default date attribute descending.""" + cf_stream, flows = _create_cf_stream + result = cf_stream.sort(descending=True) + assert result.entries == [flows[3], flows[2], flows[1], flows[0]] + + def test_sort_attr_amount_descending(_create_cf_stream): """sort(attr='amount', descending=True) sorts by amount descending.""" cf_stream, flows = _create_cf_stream