Skip to content

Add categorized repr to the .xrs accessor (#3476)#3477

Merged
brendancol merged 3 commits into
mainfrom
issue-3476
Jun 24, 2026
Merged

Add categorized repr to the .xrs accessor (#3476)#3477
brendancol merged 3 commits into
mainfrom
issue-3476

Conversation

@brendancol

Copy link
Copy Markdown
Contributor

Closes #3476

Printing the .xrs accessor used to show the default object repr, which
tells you nothing about what you can call. Now it lists the available
operations grouped by category, so you can see the toolset from a plain
REPL or notebook without tab completion or an LSP.

  • __repr__ renders a grouped, wrapped text listing; _repr_html_
    renders a compact table in Jupyter.
  • The grouping is parsed from the # ---- Category ---- banners already
    in the accessor source, so it can't drift from the actual method set. A
    test asserts every public method lands in exactly one category.
  • Falls back to a flat alphabetical method list if source introspection
    isn't available (e.g. a frozen build).

Example:

<XrsSpatialDataArrayAccessor> xarray-spatial tools for this DataArray
call as: .xrs.<name>(...)

Surface:
    slope, aspect, hillshade, curvature
Hydrology:
    flow_direction, flow_accumulation, watershed, ...
Multispectral (self = NIR band):
    ndvi, evi, arvi, savi, nbr, sipi
...

Backend coverage: not applicable. This is a pure-Python repr on the
accessor classes; it touches no array code, so numpy / cupy / dask paths
are unaffected.

Test plan:

  • pytest xrspatial/tests/test_accessor.py (79 passed)
  • New tests: category coverage of all public methods, text and HTML
    repr content, DataArray vs Dataset distinct listings, HTML escaping,
    source-unavailable fallback
  • flake8 clean on changed files

Printing `da.xrs` or `ds.xrs` now lists the available operations grouped
by category instead of showing the default object id. A helper parses the
`# ---- Category ----` banners already present in the accessor source, so
the listing stays in sync with the methods. `__repr__` renders grouped
text; `_repr_html_` renders a table for notebooks. Falls back to a flat
public-method list when source introspection is unavailable.

@brendancol brendancol left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: Add categorized repr to the .xrs accessor (#3476)

Blockers (must fix before merge)

None.

Suggestions (should fix, not blocking)

  • The text and HTML fallbacks label the no-category case differently. The
    text path prints "(categories unavailable; flat method list)"; the HTML path
    uses an "Available tools" row header (accessor.py:638 vs accessor.py:655).
    Harmless, but matching the wording would make the two paths read the same.

Nits (optional improvements)

  • accessor.py:619: _accessor_categories is lru_cached and keyed on the class.
    Only two classes ever reach it, so the cache is a two-entry memo. Fine as-is;
    noting that maxsize=None is unbounded by design, not an oversight.

What looks good

  • The parser reads the # ---- Category ---- banners already in the source, so
    the listing can't drift from the real method set. The coverage test
    (test_categories_cover_every_public_method) asserts an exact match against
    vars(cls), which also catches a future nested non-underscore def leaking into
    the listing.
  • HTML output escapes method and category names, with a dedicated test that
    injects a <script> name to prove it.
  • The source-unavailable fallback is tested with a type()-built class whose
    source inspect can't retrieve.
  • Pure-Python change with no array code, so no backend / dask / cupy / NaN
    concerns apply.

Checklist

  • Algorithm matches reference/paper: N/A (no algorithm)
  • All implemented backends produce consistent results: N/A (no array code)
  • NaN handling is correct: N/A
  • Edge cases are covered by tests: yes (fallback, HTML escaping, DataArray vs Dataset)
  • Dask chunk boundaries handled correctly: N/A
  • No premature materialization or unnecessary copies: N/A
  • Benchmark exists or is not needed: not needed (repr is interactive, not a hot path)
  • README feature matrix updated: N/A (no new function)
  • Docstrings present and accurate: yes (helpers documented; quickstart updated)

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.

@brendancol brendancol left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review (after e6d1be2)

The one Suggestion from the first pass is resolved. The text and HTML reprs
now both route through _categories_or_fallback (accessor.py:644), so the
no-banner case shows the same "Available tools" group in either path. The
fallback test was updated to assert the shared label.

No new findings. The Nit about lru_cache(maxsize=None) was a note, not a
defect, and stands as-is (two classes, intentional). Pure-Python repr change;
no backend, dask, cupy, or NaN concerns apply. Nothing blocking.

@brendancol brendancol merged commit 8bb7c16 into main Jun 24, 2026
11 checks passed
brendancol added a commit that referenced this pull request Jun 24, 2026
* Forward per-tool repr to the accessed tool (#3478)

Evaluating a single accessor tool such as da.xrs.slope returned a bound
method, whose repr embeds repr(self) and therefore the whole accessor
catalog added in #3477. Wrap public methods in a callable _AccessorTool
proxy on attribute access so the repr shows that tool's own signature and
docstring instead. Calls forward unchanged and the delegated docstring is
mirrored for help()/inspect.

* Add per-tool repr tests for the .xrs accessor (#3478)

* Document the per-tool repr in quickstart (#3478)
@brendancol brendancol deleted the issue-3476 branch June 25, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rich __repr__ for the .xrs accessor: list available tools by category

1 participant