Skip to content

Handle ValueError in _format when float conversion fails for bool-like strings#394

Open
bysiber wants to merge 1 commit intoastanin:masterfrom
bysiber:fix-maxcolwidths-bool-crash
Open

Handle ValueError in _format when float conversion fails for bool-like strings#394
bysiber wants to merge 1 commit intoastanin:masterfrom
bysiber:fix-maxcolwidths-bool-crash

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 21, 2026

Fixes #209.

When a column contains both bool-like strings ('True'/'False') and numeric strings (e.g. '1.'), the column type is detected as float. Then _format calls float('False') which raises ValueError.

The fix catches ValueError/TypeError in the float formatting branch of _format and falls back to string formatting for unconvertible values.

Before:

>>> tabulate([['False'], ['1.']])
ValueError: could not convert string to float: 'False'

After:

>>> tabulate([['False'], ['1.']])
'-----\nFalse\n    1\n-----'

When a column contains both bool-like strings ('True'/'False') and
numeric strings, the column type is detected as float. Formatting then
crashes with ValueError on float('False').

Catch ValueError and TypeError in the float branch of _format and fall
back to string formatting.

Fixes astanin#209
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.

Uncaught Python exception - ValueError: could not convert string to float: 'True'

1 participant