@@ -497,7 +497,9 @@ def _get_bounds(obj: DataArray | Dataset, key: Hashable) -> list[Hashable]:
497497
498498
499499@functools .lru_cache (maxsize = 256 )
500- def _parse_grid_mapping_attribute (grid_mapping_attr : str ) -> dict [str , list [Hashable ]]:
500+ def _parse_grid_mapping_attribute (
501+ grid_mapping_attr : str ,
502+ ) -> Mapping [str , list [Hashable ]]:
501503 """
502504 Parse a grid_mapping attribute that may contain multiple grid mappings.
503505
@@ -509,19 +511,20 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
509511 - Multiple: "spatial_ref: crs_4326: latitude longitude crs_27700: x27700 y27700"
510512 -> {"spatial_ref": [], "crs_4326": ["latitude", "longitude"], "crs_27700": ["x27700", "y27700"]}
511513
512- Returns a dictionary mapping grid mapping variable names to their associated coordinate variables.
514+ Returns a read-only mapping from grid mapping variable name to its associated
515+ coordinate variables. The result is memoized, so callers must not mutate it.
513516 """
514517 # Check if there are colons indicating multiple mappings
515518 if ":" not in grid_mapping_attr :
516- return {grid_mapping_attr .strip (): []}
519+ return Frozen ( {grid_mapping_attr .strip (): []})
517520
518521 # Use regex to parse the format
519522 # First, find all grid mapping variables (words before colons)
520523 grid_pattern = r"(?:^|\s)([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*:)"
521524 grid_mappings = re .findall (grid_pattern , grid_mapping_attr )
522525
523526 if not grid_mappings :
524- return {grid_mapping_attr .strip (): []}
527+ return Frozen ( {grid_mapping_attr .strip (): []})
525528
526529 result : dict [str , list [Hashable ]] = {}
527530
@@ -556,7 +559,7 @@ def _parse_grid_mapping_attribute(grid_mapping_attr: str) -> dict[str, list[Hash
556559def _create_grid_mapping (
557560 var_name : str ,
558561 ds : Dataset ,
559- grid_mapping_dict : dict [str , list [Hashable ]],
562+ grid_mapping_dict : Mapping [str , list [Hashable ]],
560563) -> GridMapping :
561564 """
562565 Create a GridMapping dataclass instance from a grid mapping variable.
0 commit comments