Skip to content

Fix TypeError for headers='keys' with iterator rows (#515) - #516

Open
thejedi433 wants to merge 1 commit into
astanin:masterfrom
thejedi433:fix/iterator-headers-keys
Open

thejedi433 wants to merge 1 commit into
astanin:masterfrom
thejedi433:fix/iterator-headers-keys

Conversation

@thejedi433

Copy link
Copy Markdown

Fixes #515

headers="keys" raised TypeError: object of type 'list_iterator' has no len() when rows were iterators, because _normalize_tabular_data computed the column-index headers with len(rows[0]) a few lines before the general rows = [list(r) ...] materialization. The same input renders fine with default headers, so the README's "iterable of iterables" contract was silently violated for this one headers mode.

Repro on pristine master (268615a):

tabulate([iter([1, 2])], tablefmt="plain")                  # works
tabulate([iter([1, 2])], headers="keys", tablefmt="plain")  # TypeError
tabulate((iter([r, r + 1]) for r in range(3)), headers="keys")  # TypeError

Fix: materialize rows[0] into a list when it has no __len__ before taking its length. No-op for list/tuple rows, and safe for single-use iterator rows (this also covers the generator-of-iterators case where the rows can't be replayed — verified: the values are consumed exactly once and appear in the output).

Scoping check against open PRs: none of the 25 open PRs touch #515 (searched for "515", iterator/len fixes); the closest ones (#504/#482, ragged-column handling) don't reach this line. No competing attempt on closed PRs either.

Tests: 3 regression tests in test/test_input.py (iterator rows, generator-of-iterators, ragged iterator rows). Verified RED on pre-fix tree (3 failures, TypeError) / GREEN post-fix; full suite 309 passed / 60 skipped, identical to baseline. ruff@0.6 check clean on the test file (the 10 ISC001 warnings in __init__.py are pre-existing baseline), ruff format --check clean.

_normalize_tabular_data computed column-index headers via
len(rows[0]) before rows were materialized to lists, so an
iterable of iterators (map/iter/generator rows) crashed with
TypeError: object of type 'list_iterator' has no len() even
though the same input rendered fine with the default headers.

Materialize rows[0] when it has no __len__ (no-op for list/
tuple rows, single-use-safe for generators whose rows are
themselves iterators). 3 regression tests: iterator rows,
generator-of-iterators, ragged iterator rows; verified
red->green on the full suite (309 passed, 60 skipped).
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.

headers="keys" raises TypeError when rows are iterators

1 participant