Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for issue #318 | intfmt: error for strings which contain integer values #319

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,8 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
if valtype is str:
return f"{val}"
elif valtype is int:
if isinstance(val, str):
intfmt = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe convert val to int instead, similarly to what happens when valtype is float? That way, an int in a string would still get formatted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as requested.

return format(val, intfmt)
elif valtype is bytes:
try:
Expand Down
39 changes: 39 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test output of the various forms of tabular data."""
import pytest

import tabulate as tabulate_module
from common import assert_equal, raises, skip, check_warnings
Expand Down Expand Up @@ -2638,6 +2639,44 @@ def test_intfmt():
assert_equal(expected, result)


def test_intfmt_with_string_as_integer():
"Output: integer format"
result = tabulate([[82642], ["1500"], [2463]], intfmt=",", tablefmt="plain")
expected = "82,642\n 1500\n 2,463"
assert_equal(expected, result)


@pytest.mark.skip(reason="It detects all values as floats but there are strings and integers.")
def test_intfmt_with_string_with_floats():
"Output: integer format"
result = tabulate([[82000.38], ["1500.47"], ["2463"], [92165]], intfmt=",", tablefmt="plain")
expected = "82000.4\n 1500.47\n 2463\n92,165"
assert_equal(expected, result)


def test_intfmt_with_colors():
"Regression: Align ANSI-colored values as if they were colorless."
colortable = [
("abcd", 42, "\x1b[31m42\x1b[0m"),
("elfy", 1010, "\x1b[32m1010\x1b[0m"),
]
colorheaders = ("test", "\x1b[34mtest\x1b[0m", "test")
formatted = tabulate(colortable, colorheaders, "grid", intfmt=",")
expected = "\n".join(
[
"+--------+--------+--------+",
"| test | \x1b[34mtest\x1b[0m | test |",
"+========+========+========+",
"| abcd | 42 | \x1b[31m42\x1b[0m |",
"+--------+--------+--------+",
"| elfy | 1,010 | \x1b[32m1010\x1b[0m |",
"+--------+--------+--------+",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)


def test_empty_data_with_headers():
"Output: table with empty data and headers as firstrow"
expected = ""
Expand Down