Skip to content

fix(lists): support general iterables and update typing in natural_list - #381

Open
Pxnzerr wants to merge 2 commits into
python-humanize:mainfrom
Pxnzerr:fix/natural-list-iterables
Open

fix(lists): support general iterables and update typing in natural_list#381
Pxnzerr wants to merge 2 commits into
python-humanize:mainfrom
Pxnzerr:fix/natural-list-iterables

Conversation

@Pxnzerr

@Pxnzerr Pxnzerr commented Aug 25, 2026

Copy link
Copy Markdown

Summary

natural_list() documented its argument as items (Iterable): An iterable of items, but:

  1. The type annotation was restricted to items: list[Any].
  2. The implementation relied on sequence indexing (items[0], items[:-1], items[-1]) and len(items), raising TypeError when passed generators, sets, dict_keys, iterators, or custom non-subscriptable iterables.

Changes

  • Updated natural_list type annotation to items: Iterable[Any] (importing Iterable in TYPE_CHECKING).
  • Normalized items using list comprehension (item_list = [str(item) for item in items]), safely handling any finite iterable in a single pass.
  • Added comprehensive unit test coverage in tests/test_lists.py for tuples, sets, generators, dict_keys, and range.

Validation

  • pytest tests/test_lists.py: 14 passed
  • pytest: 706 passed
  • mypy src tests/test_lists.py: clean
  • ruff check .: clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant