Skip to content

Commit

Permalink
Merge pull request #28 from GeodetischeInfrastructuur/fix-mypy
Browse files Browse the repository at this point in the history
fix type annotations and add mypy type check to pre-commit hook
  • Loading branch information
WouterVisscher authored Aug 11, 2023
2 parents 423f258 + 725c642 commit 07d1306
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
10 changes: 9 additions & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -u

print_message() {
padding_char=">"
Expand Down Expand Up @@ -27,10 +28,17 @@ print_message() {
command_available() {
command="$1"
if ! command -v "$command" &>/dev/null; then
print_message "ERROR: ${command} could not be found" 2 "ERROR" false
print_message "ERROR: ${command} could not be found - commit aborted" 2 "ERROR" false
exit 1
fi
}
command_available "mypy"
print_message "pre-commit - running mypy static type checking" 1 "" true
if ! mypy coordinates_transformation_api;then
print_message "mypy static type check failed - commit aborted" 1 "ERROR" true
exit 1
fi


command_available "black"
print_message "pre-commit - running Black formatting" 1 "" true
Expand Down
40 changes: 19 additions & 21 deletions coordinates_transformation_api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@

import yaml
from fastapi.exceptions import RequestValidationError
from geojson_pydantic import (
Feature,
FeatureCollection,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
)
from geojson_pydantic.geometries import Geometry, GeometryCollection, _GeometryBase
from geojson_pydantic.types import (
LineStringCoords,
MultiLineStringCoords,
MultiPointCoords,
MultiPolygonCoords,
PolygonCoords,
Position,
)
from geojson_pydantic import (Feature, FeatureCollection, LineString,
MultiLineString, MultiPoint, MultiPolygon, Point,
Polygon)
from geojson_pydantic.geometries import (Geometry, GeometryCollection,
_GeometryBase)
from geojson_pydantic.types import (LineStringCoords, MultiLineStringCoords,
MultiPointCoords, MultiPolygonCoords,
PolygonCoords, Position)
from pydantic import ValidationError
from pydantic_core import InitErrorDetails, PydanticCustomError
from pyproj import CRS, Transformer
Expand Down Expand Up @@ -163,10 +152,19 @@ def get_transform_callback(transformer: Transformer):
def callback(
val: Union[Tuple[float, float], Tuple[float, float, float]]
) -> Tuple[float, ...]:
if transformer.target_crs is None:
raise ValueError("transformer.target_crs is None")
dim = len(transformer.target_crs.axis_info)

if dim != None and dim != len(val):
val = val[0:dim]
if (
2 > dim > 3
): # check so we can safely cast to Tuple[float, float], Tuple[float, float, float]
raise ValueError(
f"number of dimensions of target-crs should be 2 or 3, is {dim}"
)
val = cast(
Union[Tuple[float, float], Tuple[float, float, float]], val[0:dim]
)
return tuple([float(round(x, 6)) for x in transformer.transform(*val)])

return callback
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ requires-python = ">=3.11.4"
dynamic = ["version"]

[project.optional-dependencies]
dev = ["black", "autoflake", "isort"]
# TODO: add/enable mypy when using type annotations
dev = ["black", "autoflake", "isort", "mypy"]

[build-system]
build-backend = "setuptools.build_meta"
Expand Down

0 comments on commit 07d1306

Please sign in to comment.