Type geometry as GeometryType, add type checking guide#949
Open
ecomodeller wants to merge 5 commits intomainfrom
Open
Type geometry as GeometryType, add type checking guide#949ecomodeller wants to merge 5 commits intomainfrom
ecomodeller wants to merge 5 commits intomainfrom
Conversation
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
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DataArray.geometryandDataset.geometryasGeometryType(union of all 14 geometry classes) instead ofAny— type checkers now catch typos and invalid attribute access on geometry objectsGeometryTypefrommikeio.spatialandmikeiomikeiotop-levelWhy
.geometrywas typed asAny, making type checkers and IDE autocompletion useless for geometry access. TheGeometryTypeunion already existed internally but was unused. This change gives users real type checking with zero breaking changes —isinstancenarrowing now works, and the guide shows the patterns.