Skip to content

Fix GeoJsonTooltip/GeoJsonPopup on a single Feature or null properties - #2346

Open
kavyalavti wants to merge 1 commit into
python-visualization:mainfrom
kavyalavti:fix/geojson-detail-single-feature
Open

kavyalavti wants to merge 1 commit into
python-visualization:mainfrom
kavyalavti:fix/geojson-detail-single-feature

Conversation

@kavyalavti

Copy link
Copy Markdown

Problem

GeoJson accepts a single Feature or a bare geometry, but GeoJsonTooltip and GeoJsonPopup crash on such a layer with KeyError: 'features'. A feature with "properties": null, which RFC 7946 allows, crashes with AttributeError.

Reproduce

import folium

m = folium.Map()
feature = {"type": "Feature", "geometry": {"type": "Point", "coordinates": [0, 0]}, "properties": {"name": "A"}}
folium.GeoJson(feature, tooltip=folium.GeoJsonTooltip(fields=["name"])).add_to(m)
m.get_root().render()  # KeyError: 'features'

Fix

GeoJson only converts its data to a FeatureCollection when style_function or highlight_function is given. GeoJsonDetail gains a small _geojson_features() helper that reads the features the same way convert_to_feature_collection interprets the data:

  • a FeatureCollection gives its features
  • a Feature gives a single feature
  • a bare geometry gives none

render and warn_for_geometry_collections use it, and null properties count as an empty dict. Requested fields that don't exist still fail with the existing "The field ... is not available" assertion. This mirrors the earlier TopoJson fix covered by test_topojson_non_collection_geometry_detail.

Tests

  • test_geojson_single_feature_detail covers popup and tooltip on a single Feature, with properties and with null properties.
  • test_geojson_geometry_detail_unknown_field checks that a bare geometry gives the clean "not available" error instead of KeyError.
  • All 6 new cases fail on main and pass with the fix.
  • pytest tests --ignore=tests/selenium --ignore=tests/snapshots: 402 passed. The 3 failures in tests/test_repr.py need a Chrome webdriver and fail on main locally too.
  • pre-commit hooks and mypy pass. I ran Black at the pinned 26.10.1 under Python 3.13, because Black refuses to run on the local Python 3.12.5.

AI disclosure: this change was prepared with the help of Claude Code (Anthropic). I reviewed the diff and test results.

Fixes #2345

🤖 Generated with Claude Code

GeoJsonDetail always read data["features"] and assumed the first
feature's properties is a dict. GeoJson only converts its data to a
FeatureCollection when styling is used, so a tooltip or popup on a single
Feature raised KeyError, and null properties raised AttributeError.
Read the features the same way convert_to_feature_collection interprets
the data and treat null properties as empty.

Fixes python-visualization#2345

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

GeoJsonTooltip / GeoJsonPopup crash on a single Feature or null properties

1 participant