Skip to content

Conversation

@ecomodeller
Copy link
Member

@ecomodeller ecomodeller commented Dec 22, 2025

Resolves mypy type checking errors related to xarray DataVariables type inference.

Changes:

  • Upgraded mypy from 1.16.1 to 1.19.1
  • Fixed type mismatches by updating parsing utility functions to accept Sequence[Hashable]
  • Updated type signatures in:
    • src/modelskill/utils.py (_get_name, _get_idx)
    • src/modelskill/timeseries/_point.py (_parse_point_items)
    • src/modelskill/timeseries/_track.py (_parse_track_items)
    • src/modelskill/model/_base.py (SelectedItems.parse, _parse_items)
    • src/modelskill/comparison/_comparison.py (direct xarray access)

Background: str vs Hashable

xarray's type hints specify that Dataset.data_vars returns an iterator of Hashable keys, because the xarray API technically allows any hashable type as a variable name. However, in practice, variable names are always strings.

When mypy 1.19.1 type-checks code passing these variable names to functions expecting Sequence[str], it correctly flags the type mismatch between Hashable and str.

Solution: Fix type hints at the root

Rather than converting Hashable to str at call sites (redundant), we updated the parsing utility functions to accept Sequence[Hashable]. This required:

  1. Changing _get_name() and _get_idx() to accept Sequence[Hashable]
  2. Using cast(str, ...) in _get_name() to satisfy mypy while preserving actual types (important for DataFrame integer indices)
  3. Updating all parsing functions (_parse_point_items, _parse_track_items, SelectedItems.parse) to accept Sequence[Hashable]
  4. Keeping explicit str() conversions only where needed (e.g., dict keys in _comparison.py)

This approach fixes the type system at its root while maintaining runtime compatibility with both string variable names (xarray) and integer column indices (DataFrame RangeIndex).

Fixes type checking errors with xarray DataVariables in newer mypy versions.
Cast Hashable keys to str to satisfy mypy's stricter type checking
with xarray DataVariables in Python 3.13.
@ecomodeller ecomodeller changed the title Upgrade mypy to 1.19.1 Fix mypy type errors with xarray DataVariables Dec 22, 2025
Updated parsing utility functions to accept Sequence[Hashable] rather than
Sequence[str], which properly handles xarray's DataVariables type while
maintaining compatibility with DataFrame integer indices.

- Changed _get_name() and _get_idx() to accept Sequence[Hashable]
- Used cast(str, ...) to satisfy type checker while preserving actual types
- Updated all parsing functions to accept Hashable sequences
- Kept explicit str() conversions only where needed (e.g., dict keys)
Added 'valid_items: Sequence[Hashable]' type annotations to help mypy
understand the correct type when creating lists from data_vars.
@ecomodeller ecomodeller merged commit ad19c9a into main Dec 23, 2025
2 checks passed
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.

2 participants