Skip to content

Commit 80fb7c6

Browse files
BUG: pd.show_versions: json.decoder.JSONDecodeError (pandas-dev#39766)
* BUG: Fix pd.show_versions as_json invalid JSON (pandas-dev#39701) * BUG: Separate multiple tests (39701) * BUG: Various minor improvements on test_show_versions (pandas-dev#39701) * BUG: Various test improvements to test_show_versions (pandas-dev#39701) * BUG: Remove unused function (pandas-dev#39701) * Update github issue number Co-authored-by: Marco Gorelli <[email protected]> Co-authored-by: Marco Gorelli <[email protected]>
1 parent 6ab50c4 commit 80fb7c6

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ Other
482482
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
483483
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
484484
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
485+
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
485486

486487

487488
.. ---------------------------------------------------------------------------

pandas/tests/util/test_show_versions.py

+60-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import json
2+
import os
13
import re
24

35
import pytest
46

7+
from pandas.util._print_versions import (
8+
_get_dependency_info,
9+
_get_sys_info,
10+
)
11+
512
import pandas as pd
613

714

@@ -26,11 +33,47 @@
2633
"ignore:Distutils:UserWarning"
2734
)
2835
@pytest.mark.filterwarnings("ignore:Setuptools is replacing distutils:UserWarning")
29-
def test_show_versions(capsys):
36+
def test_show_versions(tmpdir):
37+
# GH39701
38+
as_json = os.path.join(tmpdir, "test_output.json")
39+
40+
pd.show_versions(as_json=as_json)
41+
42+
with open(as_json) as fd:
43+
# check if file output is valid JSON, will raise an exception if not
44+
result = json.load(fd)
45+
46+
# Basic check that each version element is found in output
47+
expected = {
48+
"system": _get_sys_info(),
49+
"dependencies": _get_dependency_info(),
50+
}
51+
52+
assert result == expected
53+
54+
55+
def test_show_versions_console_json(capsys):
56+
# GH39701
57+
pd.show_versions(as_json=True)
58+
stdout = capsys.readouterr().out
59+
60+
# check valid json is printed to the console if as_json is True
61+
result = json.loads(stdout)
62+
63+
# Basic check that each version element is found in output
64+
expected = {
65+
"system": _get_sys_info(),
66+
"dependencies": _get_dependency_info(),
67+
}
68+
69+
assert result == expected
70+
71+
72+
def test_show_versions_console(capsys):
73+
# gh-32041
3074
# gh-32041
31-
pd.show_versions()
32-
captured = capsys.readouterr()
33-
result = captured.out
75+
pd.show_versions(as_json=False)
76+
result = capsys.readouterr().out
3477

3578
# check header
3679
assert "INSTALLED VERSIONS" in result
@@ -44,3 +87,16 @@ def test_show_versions(capsys):
4487

4588
# check optional dependency
4689
assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result)
90+
91+
92+
def test_json_output_match(capsys, tmpdir):
93+
# GH39701
94+
pd.show_versions(as_json=True)
95+
result_console = capsys.readouterr().out
96+
97+
out_path = os.path.join(tmpdir, "test_json.json")
98+
pd.show_versions(as_json=out_path)
99+
with open(out_path) as out_fd:
100+
result_file = out_fd.read()
101+
102+
assert result_console == result_file

pandas/util/_print_versions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def show_versions(as_json: Union[str, bool] = False) -> None:
115115
j = {"system": sys_info, "dependencies": deps}
116116

117117
if as_json is True:
118-
print(j)
118+
sys.stdout.writelines(json.dumps(j, indent=2))
119119
else:
120120
assert isinstance(as_json, str) # needed for mypy
121121
with codecs.open(as_json, "wb", encoding="utf8") as f:

0 commit comments

Comments
 (0)