Skip to content

Commit 7a9a16a

Browse files
committed
Minor cleanups
1. Avoid _describe and use pytest's capsys fixture to test stdout text 2. Avoid unnecessarily specifying default to mappers
1 parent 11ea76d commit 7a9a16a

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

cf_xarray/accessor.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def wrapper(
139139
error: bool = True,
140140
default: str = None,
141141
) -> List[Optional[str]]:
142+
"""
143+
This decorator will add `error` and `default` kwargs to the decorated Mapper function.
144+
"""
142145
if key not in valid_keys:
143146
if error:
144147
raise KeyError(
@@ -645,21 +648,26 @@ def __getattr__(self, attr):
645648
def plot(self):
646649
return _CFWrappedPlotMethods(self._obj, self)
647650

648-
def _describe(self):
651+
def describe(self):
652+
"""
653+
Print a string repr to screen.
654+
"""
649655
text = "Axes:\n"
650656
for key in _AXIS_NAMES:
651-
text += f"\t{key}: {_get_axis_coord(self._obj, key, error=False, default=None)}\n"
657+
text += f"\t{key}: {_get_axis_coord(self._obj, key, error=False)}\n"
652658

653659
text += "\nCoordinates:\n"
654660
for key in _COORD_NAMES:
655-
text += f"\t{key}: {_get_axis_coord(self._obj, key, error=False, default=None)}\n"
661+
text += f"\t{key}: {_get_axis_coord(self._obj, key, error=False)}\n"
656662

657663
text += "\nCell Measures:\n"
658664
for measure in _CELL_MEASURES:
659665
if isinstance(self._obj, xr.Dataset):
660666
text += f"\t{measure}: unsupported\n"
661667
else:
662-
text += f"\t{measure}: {_get_measure(self._obj, measure, error=False, default=None)}\n"
668+
text += (
669+
f"\t{measure}: {_get_measure(self._obj, measure, error=False)}\n"
670+
)
663671

664672
text += "\nStandard Names:\n"
665673
if isinstance(self._obj, xr.DataArray):
@@ -670,13 +678,7 @@ def _describe(self):
670678
text += "\n".join(
671679
textwrap.wrap(f"{stdnames!r}", 70, break_long_words=False)
672680
)
673-
return text
674-
675-
def describe(self):
676-
"""
677-
Print a string repr to screen.
678-
"""
679-
print(self._describe())
681+
print(text)
680682

681683
def get_valid_keys(self) -> Set[str]:
682684
"""

cf_xarray/tests/test_accessor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ def test_dicts():
5151
assert actual == expected
5252

5353

54-
def test_describe():
55-
actual = airds.cf._describe()
54+
def test_describe(capsys):
55+
airds.cf.describe()
56+
actual = capsys.readouterr().out
5657
expected = (
5758
"Axes:\n\tX: ['lon']\n\tY: ['lat']\n\tZ: [None]\n\tT: ['time']\n"
5859
"\nCoordinates:\n\tlongitude: ['lon']\n\tlatitude: ['lat']"
5960
"\n\tvertical: [None]\n\ttime: ['time']\n"
6061
"\nCell Measures:\n\tarea: unsupported\n\tvolume: unsupported\n"
61-
"\nStandard Names:\n\t['air_temperature', 'latitude', 'longitude', 'time']"
62+
"\nStandard Names:\n\t['air_temperature', 'latitude', 'longitude', 'time']\n"
6263
)
6364
assert actual == expected
6465

0 commit comments

Comments
 (0)