Skip to content

lib: fix incorrect backend detection in get_backend()#7240

Open
Photon079 wants to merge 2 commits intoOSGeo:mainfrom
Photon079:fix/get-backend-bug
Open

lib: fix incorrect backend detection in get_backend()#7240
Photon079 wants to merge 2 commits intoOSGeo:mainfrom
Photon079:fix/get-backend-bug

Conversation

@Photon079
Copy link
Copy Markdown

Previously, get_backend() returned "ipyleaflet" when folium import failed, without validating whether the object was actually an ipyleaflet.Map. This could lead to incorrect backend selection and failures in layer operations.

This fix:

  • Properly checks object types for both folium and ipyleaflet
  • Raises clear ValueError when neither backend matches
  • Adds comprehensive tests covering all edge cases
  • Follows GRASS coding standards

This issue was discovered while writing tests for grass.jupyter as part of the GSoC test-of-skills tasks. I have also opened issue #7239 for tracking this bug.

Previously, get_backend() would return 'ipyleaflet' whenever folium import
failed, without checking if the object was an ipyleaflet.Map.
This could cause crashes in Layer.add_to() methods when wrong backend
was selected.

This fix:
- Properly checks object types for both folium and ipyleaflet
- Raises clear ValueError when neither backend matches
- Adds comprehensive tests covering all edge cases
- Follows GRASS coding standards

Bug discovered while writing tests - original logic made dangerous
assumptions that could break map functionality.

AI assistance used for codebase understanding and test design.
All code changes and testing were performed by the contributor.
@Photon079 Photon079 changed the title Fix incorrect backend detection in get_backend() when imports fail python.jupyter: fix incorrect backend detection in get_backend() Mar 30, 2026
@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite notebook labels Mar 30, 2026
@Photon079 Photon079 changed the title python.jupyter: fix incorrect backend detection in get_backend() lib: fix incorrect backend detection in get_backend() Mar 30, 2026
Copy link
Copy Markdown
Contributor

@petrasovaa petrasovaa left a comment

Choose a reason for hiding this comment

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

Please use pre-commit to fix any formatting.

Comment on lines +3 to +4
import folium
import ipyleaflet
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The tests will hard-fail in environments without folium/ipyleaflet instead of being skipped gracefully

Comment thread python/grass/jupyter/interactivemap.py
Comment thread python/grass/jupyter/interactivemap.py Outdated
Comment on lines +53 to +54
error_msg = "No supported mapping backend found"
raise ValueError(error_msg)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just make it a single line.
Also, the message is somewhat misleading, it sounds like a missing library, not a wrong object type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Correction, this may be enforced by a ruff rule, so this is probably correct.

@Photon079
Copy link
Copy Markdown
Author

hey @petrasovaa thank you for reviewing , i am working on these things and will update soon

* Skip tests gracefully when mapping libraries are missing
* Update ValueError exception message wording
* Fix docstring alignment in get_backend()
@Photon079 Photon079 force-pushed the fix/get-backend-bug branch from 2608440 to df5731d Compare April 8, 2026 19:27
@Photon079
Copy link
Copy Markdown
Author

Hey @petrasovaa, thanks for the review! I've pushed updates to address your feedback:

Used pytest.importorskip so tests skip gracefully if the mapping libraries are missing.
Fixed the misaligned docstring and successfully ran pre-commit.
Clarified the ValueError message to point out the unsupported backend object type

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