Skip to content

Commit e6d1be2

Browse files
committed
Address review: unify repr fallback wording (#3476)
The text and HTML reprs used different labels when source banners can't be parsed. Both now route through a shared `_categories_or_fallback` helper that emits a single "Available tools" group, so the two paths read the same.
1 parent baeb688 commit e6d1be2

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

xrspatial/accessor.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -637,20 +637,27 @@ def _accessor_kind(cls):
637637
return 'Dataset' if cls.__name__.endswith('DatasetAccessor') else 'DataArray'
638638

639639

640+
# Single category used by both reprs when the source banners can't be parsed.
641+
_FALLBACK_CATEGORY = 'Available tools'
642+
643+
644+
def _categories_or_fallback(cls):
645+
"""Parsed categories, or one flat ``Available tools`` group as a fallback."""
646+
categories = _accessor_categories(cls)
647+
if categories:
648+
return categories
649+
return ((_FALLBACK_CATEGORY, _public_accessor_methods(cls)),)
650+
651+
640652
def _accessor_repr_text(cls):
641653
"""Plain-text repr listing the accessor's operations by category."""
642654
lines = [
643655
f"<{cls.__name__}> xarray-spatial tools for this {_accessor_kind(cls)}",
644656
"call as: .xrs.<name>(...)",
645657
"",
646658
]
647-
categories = _accessor_categories(cls)
648-
if not categories:
649-
lines.append("(categories unavailable; flat method list)")
650-
categories = (("", _public_accessor_methods(cls)),)
651-
for name, methods in categories:
652-
if name:
653-
lines.append(f"{name}:")
659+
for name, methods in _categories_or_fallback(cls):
660+
lines.append(f"{name}:")
654661
for chunk in textwrap.wrap(
655662
", ".join(methods), width=74,
656663
break_long_words=False, break_on_hyphens=False,
@@ -661,11 +668,8 @@ def _accessor_repr_text(cls):
661668

662669
def _accessor_repr_html(cls):
663670
"""Jupyter HTML repr: the same listing as a compact table."""
664-
categories = _accessor_categories(cls)
665-
if not categories:
666-
categories = (("Available tools", _public_accessor_methods(cls)),)
667671
rows = []
668-
for name, methods in categories:
672+
for name, methods in _categories_or_fallback(cls):
669673
methods_html = ", ".join(
670674
f"<code>{html.escape(m)}</code>" for m in methods
671675
)

xrspatial/tests/test_accessor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,10 @@ def test_categories_fallback_when_source_unavailable():
621621
"""A class with no retrievable source yields a flat fallback listing."""
622622
Dummy = type('DummyAccessor', (), {'foo': lambda self: None})
623623
assert _accessor_categories(Dummy) == ()
624+
# Both reprs fall back to the same single "Available tools" group.
624625
text = _accessor_repr_text(Dummy)
626+
assert 'Available tools:' in text
625627
assert 'foo' in text
626-
assert 'flat method list' in text
627628
html_out = _accessor_repr_html(Dummy)
629+
assert 'Available tools' in html_out
628630
assert '<code>foo</code>' in html_out

0 commit comments

Comments
 (0)