Skip to content

Commit 1123326

Browse files
committed
Merge branch 'alexdev' of https://github.com/alexprincel/pandas into pandas_alex
2 parents fc9fdba + 6337f84 commit 1123326

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ Other
461461
- Bug in :class:`Styler` where ``subset`` arg in methods raised an error for some valid multiindex slices (:issue:`33562`)
462462
- :class:`Styler` rendered HTML output minor alterations to support w3 good code standard (:issue:`39626`)
463463
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
464+
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
464465

465466

466467
.. ---------------------------------------------------------------------------

pandas/tests/util/test_show_versions.py

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import json
2+
import os
13
import re
24

35
import pytest
46

7+
from pandas.util._print_versions import _get_dependency_info, _get_sys_info
8+
59
import pandas as pd
610

711

@@ -26,21 +30,64 @@
2630
"ignore:Distutils:UserWarning"
2731
)
2832
@pytest.mark.filterwarnings("ignore:Setuptools is replacing distutils:UserWarning")
29-
def test_show_versions(capsys):
33+
@pytest.mark.parametrize("as_json", [True, False, "test_output.json"])
34+
def test_show_versions(capsys, as_json, tmpdir):
3035
# gh-32041
31-
pd.show_versions()
36+
if isinstance(as_json, str):
37+
as_json = os.path.join(tmpdir, as_json)
38+
39+
pd.show_versions(as_json=as_json)
3240
captured = capsys.readouterr()
3341
result = captured.out
42+
string_check = result
43+
44+
# check valid json is printed to the console if as_json is True
45+
if as_json is True:
46+
json.loads(result)
47+
48+
# check header for non-JSON console output
49+
elif as_json is False:
50+
assert "INSTALLED VERSIONS" in result
51+
52+
# check full commit hash
53+
assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result)
54+
55+
# check required dependency
56+
# 2020-12-09 npdev has "dirty" in the tag
57+
assert re.search(r"numpy\s*:\s([0-9\.\+a-g\_]|dev)+(dirty)?\n", result)
58+
59+
# check optional dependency
60+
assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result)
61+
62+
elif isinstance(as_json, str):
63+
# make sure that the file was created
64+
assert os.path.exists(as_json)
65+
66+
with open(as_json) as fd:
67+
contents = fd.readlines()
68+
str_contents = "".join(contents)
69+
70+
# make sure that there was output to the file
71+
assert str_contents
72+
73+
# check if file output is valid JSON
74+
json.loads(str_contents)
75+
76+
# prepare string for checking for specific keys
77+
string_check = str_contents
78+
79+
# Basic check that each version element is found in output
80+
version_elements = {**_get_sys_info, **_get_dependency_info}
81+
assert all(v in string_check and k in string_check for k, v in version_elements)
3482

35-
# check header
36-
assert "INSTALLED VERSIONS" in result
3783

38-
# check full commit hash
39-
assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result)
84+
def test_json_output_match(capsys, tmpdir):
85+
pd.show_versions(as_json=True)
86+
result_console = capsys.readouterr().out
4087

41-
# check required dependency
42-
# 2020-12-09 npdev has "dirty" in the tag
43-
assert re.search(r"numpy\s*:\s([0-9\.\+a-g\_]|dev)+(dirty)?\n", result)
88+
out_path = os.path.join(tmpdir, "test_json.json")
89+
pd.show_versions(as_json=out_path)
90+
with open(out_path) as out_fd:
91+
result_file = "".join(out_fd.readlines())
4492

45-
# check optional dependency
46-
assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result)
93+
assert result_console == result_file

pandas/util/_print_versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def show_versions(as_json: Union[str, bool] = False) -> None:
107107
j = {"system": sys_info, "dependencies": deps}
108108

109109
if as_json is True:
110-
print(j)
110+
sys.stdout.writelines(json.dumps(j, indent=2))
111111
else:
112112
assert isinstance(as_json, str) # needed for mypy
113113
with codecs.open(as_json, "wb", encoding="utf8") as f:

0 commit comments

Comments
 (0)