From 0d737e378f83c3e4511293e76ed4c0c58a7b371c Mon Sep 17 00:00:00 2001 From: Charlie Tonneslan Date: Thu, 21 May 2026 07:47:28 -0400 Subject: [PATCH] Return an empty string from natural_list for an empty list natural_list([]) fell through to the >=3-items branch, which does items[-1], so it raised IndexError on an empty list instead of returning "". Guard the empty case up front. Signed-off-by: Charlie Tonneslan --- src/humanize/lists.py | 2 ++ tests/test_lists.py | 1 + 2 files changed, 3 insertions(+) diff --git a/src/humanize/lists.py b/src/humanize/lists.py index 543a32f1..525f0e33 100644 --- a/src/humanize/lists.py +++ b/src/humanize/lists.py @@ -28,6 +28,8 @@ def natural_list(items: list[Any]) -> str: Returns: str: A string with commas and 'and' in the right places. """ + if not items: + return "" if len(items) == 1: return str(items[0]) elif len(items) == 2: diff --git a/tests/test_lists.py b/tests/test_lists.py index a39d3442..cc514f32 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -12,6 +12,7 @@ ([["one", "two", "three"]], "one, two and three"), ([["one", "two"]], "one and two"), ([["one"]], "one"), + ([[]], ""), ([[""]], ""), ([[1, 2, 3]], "1, 2 and 3"), ([[1, "two"]], "1 and two"),