Skip to content

Commit f6b6c15

Browse files
CLN: clean-up show_versions and consistently use null for json output (#32042)
1 parent 821aa25 commit f6b6c15

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

pandas/_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
Label = Optional[Hashable]
6565
Level = Union[Label, int]
6666
Ordered = Optional[bool]
67-
JSONSerializable = Union[PythonScalar, List, Dict]
67+
JSONSerializable = Optional[Union[PythonScalar, List, Dict]]
6868
Axes = Collection
6969

7070
# For functions like rename that convert one label to another

pandas/util/_print_versions.py

+45-49
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import platform
66
import struct
77
import sys
8-
from typing import List, Optional, Tuple, Union
8+
from typing import Dict, Optional, Union
99

10+
from pandas._typing import JSONSerializable
1011
from pandas.compat._optional import VERSIONS, _get_version, import_optional_dependency
1112

1213

@@ -21,43 +22,32 @@ def _get_commit_hash() -> Optional[str]:
2122
return versions["full-revisionid"]
2223

2324

24-
def get_sys_info() -> List[Tuple[str, Optional[Union[str, int]]]]:
25+
def _get_sys_info() -> Dict[str, JSONSerializable]:
2526
"""
26-
Returns system information as a list
27+
Returns system information as a JSON serializable dictionary.
28+
"""
29+
uname_result = platform.uname()
30+
language_code, encoding = locale.getlocale()
31+
return {
32+
"commit": _get_commit_hash(),
33+
"python": ".".join(str(i) for i in sys.version_info),
34+
"python-bits": struct.calcsize("P") * 8,
35+
"OS": uname_result.system,
36+
"OS-release": uname_result.release,
37+
"Version": uname_result.version,
38+
"machine": uname_result.machine,
39+
"processor": uname_result.processor,
40+
"byteorder": sys.byteorder,
41+
"LC_ALL": os.environ.get("LC_ALL"),
42+
"LANG": os.environ.get("LANG"),
43+
"LOCALE": {"language-code": language_code, "encoding": encoding},
44+
}
45+
46+
47+
def _get_dependency_info() -> Dict[str, JSONSerializable]:
48+
"""
49+
Returns dependency information as a JSON serializable dictionary.
2750
"""
28-
blob: List[Tuple[str, Optional[Union[str, int]]]] = []
29-
30-
# get full commit hash
31-
commit = _get_commit_hash()
32-
33-
blob.append(("commit", commit))
34-
35-
try:
36-
(sysname, nodename, release, version, machine, processor) = platform.uname()
37-
blob.extend(
38-
[
39-
("python", ".".join(map(str, sys.version_info))),
40-
("python-bits", struct.calcsize("P") * 8),
41-
("OS", f"{sysname}"),
42-
("OS-release", f"{release}"),
43-
# FIXME: dont leave commented-out
44-
# ("Version", f"{version}"),
45-
("machine", f"{machine}"),
46-
("processor", f"{processor}"),
47-
("byteorder", f"{sys.byteorder}"),
48-
("LC_ALL", f"{os.environ.get('LC_ALL', 'None')}"),
49-
("LANG", f"{os.environ.get('LANG', 'None')}"),
50-
("LOCALE", ".".join(map(str, locale.getlocale()))),
51-
]
52-
)
53-
except (KeyError, ValueError):
54-
pass
55-
56-
return blob
57-
58-
59-
def show_versions(as_json=False):
60-
sys_info = get_sys_info()
6151
deps = [
6252
"pandas",
6353
# required
@@ -86,39 +76,45 @@ def show_versions(as_json=False):
8676
"IPython",
8777
"pandas_datareader",
8878
]
89-
9079
deps.extend(list(VERSIONS))
91-
deps_blob = []
9280

81+
result: Dict[str, JSONSerializable] = {}
9382
for modname in deps:
9483
mod = import_optional_dependency(
9584
modname, raise_on_missing=False, on_version="ignore"
9685
)
97-
ver: Optional[str]
98-
if mod:
99-
ver = _get_version(mod)
100-
else:
101-
ver = None
102-
deps_blob.append((modname, ver))
86+
result[modname] = _get_version(mod) if mod else None
87+
return result
88+
89+
90+
def show_versions(as_json: Union[str, bool] = False) -> None:
91+
sys_info = _get_sys_info()
92+
deps = _get_dependency_info()
10393

10494
if as_json:
105-
j = dict(system=dict(sys_info), dependencies=dict(deps_blob))
95+
j = dict(system=sys_info, dependencies=deps)
10696

10797
if as_json is True:
10898
print(j)
10999
else:
100+
assert isinstance(as_json, str) # needed for mypy
110101
with codecs.open(as_json, "wb", encoding="utf8") as f:
111102
json.dump(j, f, indent=2)
112103

113104
else:
105+
assert isinstance(sys_info["LOCALE"], dict) # needed for mypy
106+
language_code = sys_info["LOCALE"]["language-code"]
107+
encoding = sys_info["LOCALE"]["encoding"]
108+
sys_info["LOCALE"] = f"{language_code}.{encoding}"
109+
114110
maxlen = max(len(x) for x in deps)
115111
print("\nINSTALLED VERSIONS")
116112
print("------------------")
117-
for k, stat in sys_info:
118-
print(f"{k:<{maxlen}}: {stat}")
113+
for k, v in sys_info.items():
114+
print(f"{k:<{maxlen}}: {v}")
119115
print("")
120-
for k, stat in deps_blob:
121-
print(f"{k:<{maxlen}}: {stat}")
116+
for k, v in deps.items():
117+
print(f"{k:<{maxlen}}: {v}")
122118

123119

124120
def main() -> int:

0 commit comments

Comments
 (0)