Skip to content

Add mypy pre-commit#140

Open
MarianaVZorzo wants to merge 3 commits into
ioos:mainfrom
MarianaVZorzo:add-mypy-precommit
Open

Add mypy pre-commit#140
MarianaVZorzo wants to merge 3 commits into
ioos:mainfrom
MarianaVZorzo:add-mypy-precommit

Conversation

@MarianaVZorzo

Copy link
Copy Markdown

Adds the mypy pre-commit hook configuration following the setup used in erddapy.

Comment thread .pre-commit-config.yaml Outdated
hooks:
- id: mypy
additional_dependencies:
- types-requests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requests are not used directly here, let's remove this line for now.

Comment thread .pre-commit-config.yaml Outdated
hooks:
- id: mypy
additional_dependencies:
- types-requests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- types-requests

@ocefpaf

ocefpaf commented Jun 14, 2026

Copy link
Copy Markdown
Member

@MarianaVZorzo as expected, when we added type hinting checking, we got tons of "erros" from types that are not defined:

profiling/STOFS_benchmarks.py:35: error: Unsupported operand types for - ("Callable[[], float]" and "float")  [operator]
profiling/STOFS_benchmarks.py:40: error: Unsupported operand types for - ("Callable[[], float]" and "float")  [operator]
profiling/STOFS_benchmarks.py:46: error: Unsupported operand types for - ("Callable[[], float]" and "float")  [operator]
xarray_subset_grid/selector.py:29: error: Incompatible types in assignment (expression has type "None", base class "object" defined the type as "Callable[[], int]")  [assignment]
xarray_subset_grid/grid.py:35: error: Incompatible return value type (got "set[Never]", expected "list[str]")  [return-value]
xarray_subset_grid/grid.py:53: error: Unsupported operand types for - ("set[Any]" and "list[str]")  [operator]
xarray_subset_grid/grid.py:81: error: Argument 2 to "subset_vertical_level" of "Grid" has incompatible type "floating[_32Bit]"; expected "float"  [arg-type]
xarray_subset_grid/grid.py:83: error: Argument 2 to "subset_vertical_level" of "Grid" has incompatible type "floating[_32Bit]"; expected "float"  [arg-type]
xarray_subset_grid/grid.py:92: error: Argument 2 to "subset_vertical_level" of "Grid" has incompatible type "floating[_32Bit]"; expected "float"  [arg-type]
xarray_subset_grid/grid.py:94: error: Argument 2 to "subset_vertical_level" of "Grid" has incompatible type "floating[_32Bit]"; expected "float"  [arg-type]
xarray_subset_grid/grid.py:137: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grid.py:137: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grid.py:137: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grid.py:151: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grid.py:151: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grid.py:151: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grid.py:171: error: Argument 2 to "compute_polygon_subset_selector" of "Grid" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "list[tuple[float, float]]"  [arg-type]
xarray_subset_grid/grid.py:187: error: Argument 2 to "compute_polygon_subset_selector" of "Grid" has incompatible type "list[tuple[float, float]] | ndarray[tuple[Any, ...], dtype[Any]]"; expected "list[tuple[float, float]]"  [arg-type]
xarray_subset_grid/grids/ugrid.py:129: error: Return type "set[str]" of "grid_vars" incompatible with return type "list[str]" in supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/ugrid.py:166: error: Incompatible types in assignment (expression has type "set[Any]", variable has type "list[Any]")  [assignment]
xarray_subset_grid/grids/ugrid.py:174: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grids/ugrid.py:174: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grids/ugrid.py:174: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grids/ugrid.py:285: error: Incompatible default for parameter "face_edge_connectivity" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grids/ugrid.py:285: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grids/ugrid.py:285: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grids/ugrid.py:418: error: Item "str" of "str | Any" has no attribute "values"  [union-attr]
xarray_subset_grid/grids/ugrid.py:441: error: Item "str" of "str | None" has no attribute "values"  [union-attr]
xarray_subset_grid/grids/ugrid.py:441: error: Item "None" of "str | None" has no attribute "values"  [union-attr]
xarray_subset_grid/grids/sgrid.py:73: error: Return type "set[str]" of "grid_vars" incompatible with return type "list[str]" in supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/sgrid.py:95: error: Need type annotation for "dims" (hint: "dims: list[<type>] = ...")  [var-annotated]
xarray_subset_grid/grids/sgrid.py:98: error: Incompatible types in assignment (expression has type "set[str]", variable has type "list[str]")  [assignment]
xarray_subset_grid/grids/sgrid.py:103: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:103: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grids/sgrid.py:103: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grids/sgrid.py:133: error: Item "None" of "Any | None" has no attribute "shape"  [union-attr]
xarray_subset_grid/grids/sgrid.py:135: error: Item "None" of "Any | None" has no attribute "shape"  [union-attr]
xarray_subset_grid/grids/sgrid.py:145: error: Incompatible types in assignment (expression has type "str", variable has type "list[tuple[list[str], list[str]]]")  [assignment]
xarray_subset_grid/grids/sgrid.py:147: error: Argument 1 to "set" has incompatible type "list[tuple[list[str], list[str]]]"; expected "Iterable[str]"  [arg-type]
xarray_subset_grid/grids/sgrid.py:155: error: Item "None" of "Any | None" has no attribute "dims"  [union-attr]
xarray_subset_grid/grids/sgrid.py:156: error: List comprehension has incompatible type List[int]; expected List[str]  [misc]
xarray_subset_grid/grids/sgrid.py:157: error: Value of type variable "_ScalarT" of "__call__" of "_ConstructorEmpty" cannot be "bool"  [type-var]
xarray_subset_grid/grids/sgrid.py:157: error: Item "None" of "Any | None" has no attribute "shape"  [union-attr]
xarray_subset_grid/grids/sgrid.py:162: error: Item "None" of "Any | None" has no attribute "dims"  [union-attr]
xarray_subset_grid/grids/sgrid.py:184: error: Incompatible types in assignment (expression has type "list[str]", variable has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:186: error: Incompatible types in assignment (expression has type "list[str]", variable has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:206: error: Incompatible types in assignment (expression has type "dict[str, Any]", target has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:225: error: Incompatible types in assignment (expression has type "list[str]", variable has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:227: error: Incompatible types in assignment (expression has type "list[str]", variable has type "str")  [assignment]
xarray_subset_grid/grids/sgrid.py:232: error: Argument 1 to "zip" has incompatible type "list[str]"; expected "Iterable[list[str]]"  [arg-type]
xarray_subset_grid/grids/regular_grid_2d.py:62: error: Return type "set[str]" of "grid_vars" incompatible with return type "list[str]" in supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/regular_grid_2d.py:91: error: Incompatible default for parameter "name" (default has type "None", parameter has type "str")  [assignment]
xarray_subset_grid/grids/regular_grid_2d.py:91: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
xarray_subset_grid/grids/regular_grid_2d.py:91: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
xarray_subset_grid/grids/regular_grid.py:130: error: Return type "set[str]" of "grid_vars" incompatible with return type "list[str]" in supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/regular_grid.py:158: error: Signature of "compute_polygon_subset_selector" incompatible with supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/regular_grid.py:158: note:      Superclass:
xarray_subset_grid/grids/regular_grid.py:158: note:          def compute_polygon_subset_selector(self, ds: Any, polygon: list[tuple[float, float]], name: str = ...) -> Selector
xarray_subset_grid/grids/regular_grid.py:158: note:      Subclass:
xarray_subset_grid/grids/regular_grid.py:158: note:          def compute_polygon_subset_selector(self, ds: Any, polygon: list[tuple[float, float]]) -> Selector
xarray_subset_grid/grids/regular_grid.py:164: error: Incompatible types in assignment (expression has type "ndarray[tuple[Any, ...], dtype[Any]]", variable has type "list[tuple[float, float]]")  [assignment]
xarray_subset_grid/grids/regular_grid.py:172: error: Signature of "compute_bbox_subset_selector" incompatible with supertype "xarray_subset_grid.grid.Grid"  [override]
xarray_subset_grid/grids/regular_grid.py:172: note:      Superclass:
xarray_subset_grid/grids/regular_grid.py:172: note:          def compute_bbox_subset_selector(self, ds: Any, bbox: tuple[float, float, float, float], name: str = ...) -> Selector
xarray_subset_grid/grids/regular_grid.py:172: note:      Subclass:
xarray_subset_grid/grids/regular_grid.py:172: note:          def compute_bbox_subset_selector(self, ds: Any, bbox: tuple[float, float, float, float]) -> Selector
xarray_subset_grid/accessor.py:35: error: Argument 2 to "insert" of "list" has incompatible type "Grid"; expected "type[Grid]"  [arg-type]
xarray_subset_grid/accessor.py:47: error: Cannot instantiate abstract class "Grid" with abstract attributes "compute_polygon_subset_selector", "data_vars", "grid_vars", "name" and "recognize"  [abstract]
xarray_subset_grid/accessor.py:82: error: Item "None" of "Grid | None" has no attribute "data_vars"  [union-attr]
xarray_subset_grid/accessor.py:99: error: Incompatible return value type (got "list[str]", expected "set[str]")  [return-value]
Found 50 errors in 8 files (checked 28 source files)

We should slowly fix those. I'll send an example ASAP for you to follow on how to add types to the library. Some may be low hanging fruit, others a bit more involved. This is not something we want to do in a single PR.

@MarianaVZorzo

Copy link
Copy Markdown
Author

Thanks, Filipe! I removed types-requests. I see the mypy errors and I'll wait for your example and the next steps :)

This was referenced Jun 15, 2026
@ChrisBarker-NOAA

Copy link
Copy Markdown
Contributor

Question: why a pre-commit hook, rather than a CI job (that's allowed to fail) -- particularly this early in the game, where it's a long way from passing ?

@ocefpaf

ocefpaf commented Jun 15, 2026

Copy link
Copy Markdown
Member

Question: why a pre-commit hook, rather than a CI job (that's allowed to fail) -- particularly this early in the game, where it's a long way from passing ?

That is "pre-commit-ci" it has the advantage to save on GitHub API tokens. We can run a CI test on GitHub actions for it, but the author deprecated it in liue for pre-commit CI. With that said, we can run the mypy only check with GitHub Actions CIs.

@ChrisBarker-NOAA

Copy link
Copy Markdown
Contributor

I'm still a bit confused about what "pre-commit-ci" is -- but I did look into a bit, and this does seem to make sense for this -- thanks!

@ocefpaf

ocefpaf commented Jun 15, 2026

Copy link
Copy Markdown
Member

I'm still a bit confused about what "pre-commit-ci" is -- but I did look into a bit, and this does seem to make sense for this -- thanks!

Sorry I was not clear, but the gist of it is that pre-commit-ci runs the pre-commit hooks for us as part of a PR test matrix. That helps folks who do not have the hooks installed locally.

I do prefer to run it in GitHub Actions, but the author deprecated it :-/

With that said, pre-commit-ci is not bad. It is fast, isolated and safe, and save us GH tokens. (We don't get rate limited here, but in big orgs that last one sure comes in handy.)

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.

3 participants