Skip to content

Type geometry as GeometryType, add type checking guide#949

Open
ecomodeller wants to merge 5 commits intomainfrom
docs/typing-guide
Open

Type geometry as GeometryType, add type checking guide#949
ecomodeller wants to merge 5 commits intomainfrom
docs/typing-guide

Conversation

@ecomodeller
Copy link
Copy Markdown
Member

@ecomodeller ecomodeller commented Mar 25, 2026

Summary

  • Type DataArray.geometry and Dataset.geometry as GeometryType (union of all 14 geometry classes) instead of Any — type checkers now catch typos and invalid attribute access on geometry objects
  • Define and export GeometryType from mikeio.spatial and mikeio
  • Export all public geometry classes from mikeio top-level
  • Add concise type checking guide to docs

Why

.geometry was typed as Any, making type checkers and IDE autocompletion useless for geometry access. The GeometryType union already existed internally but was unused. This change gives users real type checking with zero breaking changes — isinstance narrowing now works, and the guide shows the patterns.

New user-guide page showing how to narrow types for mikeio.open() and
.geometry, with concrete mypy before/after examples.

Re-export all public geometry classes (Geometry0D, GeometryPoint2D,
GeometryPoint3D, GeometryFMVerticalColumn, GeometryFMPointSpectrum,
GeometryFMLineSpectrum, GeometryFMAreaSpectrum) from mikeio top-level
so users have a single canonical import path.
Replace `Any` annotation on DataArray.geometry and Dataset.geometry with
`GeometryType` — a union of all 14 geometry classes. This means type
checkers now catch typos and invalid attribute access on geometry objects
without requiring users to narrow first.

- Define GeometryType in spatial/__init__.py and export from mikeio
- Add typed geometry properties to specialized plotter classes
- Add isinstance guards in Dfs/Dfsu writers where geometry type is known
- Simplify typing guide to reflect the improvement
@ecomodeller ecomodeller changed the title Add type checking guide, export all geometry classes Type geometry as GeometryType, add type checking guide Mar 26, 2026
Tests access .geometry attributes (nx, x, y, find_index, etc.) without
type narrowing, causing 1560 union-attr errors after the GeometryType
change.
Add assert isinstance() before geometry-specific attribute access
in test files instead of excluding them from mypy. Fix is_layered
check in _plot_FM_map to use GeometryFM3D (was dead code on
GeometryFM2D).
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.

1 participant