Skip to content

Conversation

@Rhushya
Copy link

@Rhushya Rhushya commented Feb 9, 2026

@petrasovaa @wenzeslaus @cwhite911

Description

This PR adds test coverage for grass.jupyter.utils and refactors several functions to use the new Tools API.

Changes:

  • Added utils_test.py with 18 pytest tests for pure Python utility functions
  • Refactored 5 functions in utils.py from gs.run_command/gs.read_command to the new Tools API

Added Stuff

  • Added tests for new functionality
  • Code follows existing project patterns
  • Functions refactored to use recommended Tools API

Copilot AI review requested due to automatic review settings February 9, 2026 09:53
@github-actions github-actions bot added Python Related code is in Python libraries tests Related to Test Suite notebook labels Feb 9, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds unit tests for GRASS Jupyter pure-Python utility helpers and refactors several GRASS-module-calling utilities in grass.jupyter.utils to use the newer grass.tools.Tools API.

Changes:

  • Added utils_test.py with pytest coverage for pure-Python helpers (rendering size, command parsing, HTML formatting).
  • Refactored region and projection-related helpers in utils.py from grass.script run_command/read_command/parse_command to Tools.
  • Expanded get_region_bounds_latlon() and update_region() to accept an optional env parameter.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
python/grass/jupyter/utils.py Refactors multiple GRASS tool invocations to Tools, adds env support for region helpers.
python/grass/jupyter/tests/utils_test.py Introduces pytest unit tests for pure-Python utility functions in grass.jupyter.utils.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

current = gs.region(env=env)
tools = Tools(env=env)
return tools.g_region(
flags="p" if gs.locn_is_latlong() else "pa",
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

update_region() accepts an env parameter and uses it for gs.region() and Tools(env=env), but gs.locn_is_latlong() is called without env. When env is provided for a different session/location, the chosen flags ("p" vs "pa") may be based on the wrong location. Pass env=env to gs.locn_is_latlong() for consistent behavior.

Suggested change
flags="p" if gs.locn_is_latlong() else "pa",
flags="p" if gs.locn_is_latlong(env=env) else "pa",

Copilot uses AI. Check for mistakes.
Comment on lines +312 to 324
tools = Tools(env=env)
result = tools.r_proj(
flags="g",
input=raster,
mapset=mapset,
project=location,
dbase=dbase,
env=env,
).strip()
params = gs.parse_key_val(output, vsep=" ")
output = gs.read_command("g.region", flags="ug", env=env, **params)
keyval = gs.parse_key_val(output, val_type=float)
cell_ns = (keyval["n"] - keyval["s"]) / keyval["rows"]
cell_ew = (keyval["e"] - keyval["w"]) / keyval["cols"]
)
params = gs.parse_key_val(result.text.strip(), vsep=" ")
region = tools.g_region(flags="u", format="json", **params).json
cell_ns = (region["n"] - region["s"]) / region["rows"]
cell_ew = (region["e"] - region["w"]) / region["cols"]
return (cell_ew + cell_ns) / 2.0
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

The refactor of estimate_resolution() to the Tools API isn’t covered by tests in python/grass/jupyter/tests (no references found). Adding an integration test (using the existing session fixtures) would help catch regressions in the r.proj/g.region interaction and the expected output parsing.

Copilot uses AI. Check for mistakes.
cell_ew = (keyval["e"] - keyval["w"]) / keyval["cols"]
)
params = gs.parse_key_val(result.text.strip(), vsep=" ")
region = tools.g_region(flags="u", format="json", **params).json
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

In estimate_resolution(), tools.g_region(flags="u", format="json", **params) is likely to produce no stdout (g.region without a printing flag), so Tools will return None and accessing .json will fail at runtime. Add a flag which produces output (e.g., include "p" or "g"), or switch to parsing key-value output (e.g., use .keyval) while preserving the previous behavior of g.region -ug.

Suggested change
region = tools.g_region(flags="u", format="json", **params).json
region = tools.g_region(flags="ug", format="json", **params).json

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +8
import pytest

Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
@wenzeslaus
Copy link
Member

At least some of the copilot comments seem relevant, please get those addressed first.

@wenzeslaus
Copy link
Member

Looking at this more closely, the tests cover different parts of code, not what was changed. The migration is very basic/naive. Candidate for closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries notebook Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants