FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: accept tuples (and any sequence) in _expand_iterable, not just lists by koteshyelamati · Pull Request #437 · astanin/python-tabulate · GitHub

fix: accept tuples (and any sequence) in _expand_iterable, not just lists - #437

Open
koteshyelamati wants to merge 1 commit into
astanin:masterfrom
koteshyelamati:patch-1
Open

fix: accept tuples (and any sequence) in _expand_iterable, not just lists#437
koteshyelamati wants to merge 1 commit into
astanin:masterfrom
koteshyelamati:patch-1

Conversation

Copy link
Copy Markdown

Fixes #434

Problem

tabulate() raises TypeError when sequence-valued parameters like rowalign or maxheadercolwidths are passed as tuples instead of lists:

from tabulate import tabulate

tabulate([[1, 2], [3, 4]], rowalign=("top", "bottom"))
# TypeError: can only concatenate tuple (not "list") to tuple

tabulate([[1, 2], [3, 4]], rowalign=["top", "bottom"])  # works

All other sequence-valued parameters (colalign, floatfmt, maxcolwidths, etc.) accept tuples, so this is an inconsistency.

Root cause

_expand_iterable does original + [default] * n. When original is a tuple, tuple + list raises TypeError.

Fix

Coerce original to a list before concatenation:

# before
return original + [default] * (num_desired - len(original))
# after
return list(original) + [default] * (num_desired - len(original))

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

tabulate() raises TypeError when rowalign is passed as a tuple instead of a list

1 participant


Back | FazBrowse Home | New Git URL