Skip to content

Support general iterables in natural_list - #381

Merged
hugovk merged 2 commits into
python-humanize:mainfrom
Pxnzerr:fix/natural-list-iterables
Sep 16, 2026
Merged

hugovk merged 2 commits into
python-humanize:mainfrom
Pxnzerr:fix/natural-list-iterables

Conversation

@Pxnzerr

@Pxnzerr Pxnzerr commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

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

@hugovk
hugovk force-pushed the fix/natural-list-iterables branch from 3de92ef to da44e1c Compare September 16, 2026 16:24
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.69%. Comparing base (bada0f8) to head (da44e1c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #381   +/-   ##
=======================================
  Coverage   99.69%   99.69%           
=======================================
  Files          12       12           
  Lines         980      983    +3     
=======================================
+ Hits          977      980    +3     
  Misses          3        3           
Flag Coverage Δ
macos-latest 97.55% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 97.55% <100.00%> (+<0.01%) ⬆️
windows-latest 93.18% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk hugovk added the changelog: Fixed For any bug fixes label Sep 16, 2026
@hugovk hugovk changed the title fix(lists): support general iterables and update typing in natural_list Support general iterables in natural_list Sep 16, 2026
@hugovk
hugovk merged commit ffdf407 into python-humanize:main Sep 16, 2026
43 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For any bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants