Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing in icon4pytools #267

Merged
merged 15 commits into from
Sep 21, 2023
4 changes: 2 additions & 2 deletions model/common/src/icon4py/model/common/grid/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import numpy as np
from gt4py.next.ffront.fbuiltins import int32

from gt4py.next import common
from icon4py.model.common.dimension import KDim


Expand All @@ -34,7 +34,7 @@ class VerticalModelParams:
rayleigh_damping_height: height of rayleigh damping in [m] mo_nonhydro_nml
"""

vct_a: Field[[KDim], float]
vct_a: common.Field
rayleigh_damping_height: Final[float]
index_of_damping_layer: Final[int32] = field(init=False)

Expand Down
3 changes: 2 additions & 1 deletion tools/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ dependencies = [
'icon4py-common',
'tabulate>=0.8.9',
'fprettify>=0.3.7',
'cffi>=1.5'
'cffi>=1.5',
'types-cffi>=1.15'
samkellerhals marked this conversation as resolved.
Show resolved Hide resolved
]
description = 'Tools and utilities for integrating icon4py code into the ICON model.'
dynamic = ['version']
Expand Down
6 changes: 3 additions & 3 deletions tools/src/icon4pytools/py2f/cffi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


class CffiMethod:
_registry = {}
_registry: dict[str, list[str]] = {}

@classmethod
def register(cls, func):
Expand Down Expand Up @@ -108,7 +108,7 @@ def to_fields(dim_sizes: dict[Dimension, int]):
ffi = cffi.FFI()
dim_sizes = dim_sizes

def _dim_sizes(dims: list[Dimension]) -> tuple[int, int]:
def _dim_sizes(dims: list[Dimension]) -> tuple[int | None, int | None]:
"""Extract the size of dimension from a dictionary."""
v_size = None
h_size = None
Expand Down Expand Up @@ -140,7 +140,7 @@ def _unpack(ptr, size_h, size_v, dtype) -> np.ndarray:
# TODO (magdalena) fix dtype handling use SCALARTYPE?
mem_size = ffi.sizeof(c_type)
mem_size = np.dtype(c_type).itemsize
ar = np.frombuffer(
ar = np.frombuffer( # type: ignore[call-overload]
ffi.buffer(ptr, length * mem_size),
dtype=np.dtype(c_type),
count=-1,
Expand Down
4 changes: 2 additions & 2 deletions tools/src/icon4pytools/py2f/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from pathlib import Path
from typing import Sequence

from gt4py.eve import Node, codegen
Expand Down Expand Up @@ -140,6 +140,6 @@ def generate_c_header(plugin: CffiPlugin) -> str:
return codegen.format_source("cpp", generated_code, style="LLVM")


def generate_and_write_f90_interface(build_path: str, plugin: CffiPlugin):
def generate_and_write_f90_interface(build_path: Path, plugin: CffiPlugin):
generated_code = F90InterfaceGenerator.apply(plugin)
write_string(generated_code, build_path, f"{plugin.name}.f90")
15 changes: 8 additions & 7 deletions tools/src/icon4pytools/py2f/typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
# SPDX-License-Identifier: GPL-3.0-or-later

from gt4py.next.common import Dimension
from gt4py.next.type_system.type_specifications import ScalarType
from gt4py.next.type_system.type_specifications import ScalarType, FieldType, ScalarKind
from gt4py.next.type_system.type_translation import from_type_hint


def parse_annotation(annotation) -> tuple[list[Dimension], ScalarType]:
def parse_annotation(annotation) -> tuple[list[Dimension], ScalarKind]:
type_spec = from_type_hint(annotation)

if isinstance(type_spec, ScalarType):
dtype = type_spec.kind
dims = []
return [], type_spec.kind
elif isinstance(type_spec, FieldType):
return type_spec.dims, type_spec.dtype.kind
else:
dtype = type_spec.dtype.kind
dims = type_spec.dims
return dims, dtype
raise ValueError(f"Unsupported type specification: {type_spec}")

2 changes: 1 addition & 1 deletion tools/src/icon4pytools/py2f/wrappers/diffusion_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# flake8: noqa

# We use gt4py type annotations and thus need to ignore this in MyPy
# type: ignore[valid-type]
# mypy: disable-error-code="valid-type"

"""
Wrapper module for diffusion granule.
Expand Down