From b5c61e0b5b4e9f955d55c2368cdc904f9390b288 Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Wed, 6 Dec 2023 11:13:12 +0100 Subject: [PATCH] improve typing for @compiles constructs The first parameter is in fact the self parameter referring to the function class. --- nominatim/db/sqlalchemy_functions.py | 26 +++++++++++------------ nominatim/db/sqlalchemy_types/geometry.py | 24 ++++++++++----------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/nominatim/db/sqlalchemy_functions.py b/nominatim/db/sqlalchemy_functions.py index cb04f762..8b196738 100644 --- a/nominatim/db/sqlalchemy_functions.py +++ b/nominatim/db/sqlalchemy_functions.py @@ -29,7 +29,7 @@ class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[Any]): @compiles(PlacexGeometryReverseLookuppolygon) # type: ignore[no-untyped-call, misc] -def _default_intersects(element: SaColumn, +def _default_intersects(element: PlacexGeometryReverseLookuppolygon, compiler: 'sa.Compiled', **kw: Any) -> str: return ("(ST_GeometryType(placex.geometry) in ('ST_Polygon', 'ST_MultiPolygon')" " AND placex.rank_address between 4 and 25" @@ -40,7 +40,7 @@ def _default_intersects(element: SaColumn, @compiles(PlacexGeometryReverseLookuppolygon, 'sqlite') # type: ignore[no-untyped-call, misc] -def _sqlite_intersects(element: SaColumn, +def _sqlite_intersects(element: PlacexGeometryReverseLookuppolygon, compiler: 'sa.Compiled', **kw: Any) -> str: return ("(ST_GeometryType(placex.geometry) in ('POLYGON', 'MULTIPOLYGON')" " AND placex.rank_address between 4 and 25" @@ -61,7 +61,7 @@ class IntersectsReverseDistance(sa.sql.functions.GenericFunction[Any]): @compiles(IntersectsReverseDistance) # type: ignore[no-untyped-call, misc] -def default_reverse_place_diameter(element: SaColumn, +def default_reverse_place_diameter(element: IntersectsReverseDistance, compiler: 'sa.Compiled', **kw: Any) -> str: table = element.tablename return f"({table}.rank_address between 4 and 25"\ @@ -74,7 +74,7 @@ def default_reverse_place_diameter(element: SaColumn, @compiles(IntersectsReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc] -def sqlite_reverse_place_diameter(element: SaColumn, +def sqlite_reverse_place_diameter(element: IntersectsReverseDistance, compiler: 'sa.Compiled', **kw: Any) -> str: geom1, rank, geom2 = list(element.clauses) table = element.tablename @@ -102,7 +102,7 @@ class IsBelowReverseDistance(sa.sql.functions.GenericFunction[Any]): @compiles(IsBelowReverseDistance) # type: ignore[no-untyped-call, misc] -def default_is_below_reverse_distance(element: SaColumn, +def default_is_below_reverse_distance(element: IsBelowReverseDistance, compiler: 'sa.Compiled', **kw: Any) -> str: dist, rank = list(element.clauses) return "%s < reverse_place_diameter(%s)" % (compiler.process(dist, **kw), @@ -110,7 +110,7 @@ def default_is_below_reverse_distance(element: SaColumn, @compiles(IsBelowReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc] -def sqlite_is_below_reverse_distance(element: SaColumn, +def sqlite_is_below_reverse_distance(element: IsBelowReverseDistance, compiler: 'sa.Compiled', **kw: Any) -> str: dist, rank = list(element.clauses) return "%s < 14.0 * exp(-0.2 * %s) - 0.03" % (compiler.process(dist, **kw), @@ -139,7 +139,7 @@ class IsAddressPoint(sa.sql.functions.GenericFunction[Any]): @compiles(IsAddressPoint) # type: ignore[no-untyped-call, misc] -def default_is_address_point(element: SaColumn, +def default_is_address_point(element: IsAddressPoint, compiler: 'sa.Compiled', **kw: Any) -> str: rank, hnr, name = list(element.clauses) return "(%s = 30 AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % ( @@ -149,7 +149,7 @@ def default_is_address_point(element: SaColumn, @compiles(IsAddressPoint, 'sqlite') # type: ignore[no-untyped-call, misc] -def sqlite_is_address_point(element: SaColumn, +def sqlite_is_address_point(element: IsAddressPoint, compiler: 'sa.Compiled', **kw: Any) -> str: rank, hnr, name = list(element.clauses) return "(%s = 30 AND coalesce(%s, json_extract(%s, '$.addr:housename')) IS NOT NULL)" % ( @@ -166,7 +166,7 @@ class CrosscheckNames(sa.sql.functions.GenericFunction[Any]): inherit_cache = True @compiles(CrosscheckNames) # type: ignore[no-untyped-call, misc] -def compile_crosscheck_names(element: SaColumn, +def compile_crosscheck_names(element: CrosscheckNames, compiler: 'sa.Compiled', **kw: Any) -> str: arg1, arg2 = list(element.clauses) return "coalesce(avals(%s) && ARRAY(SELECT * FROM json_array_elements_text(%s)), false)" % ( @@ -174,7 +174,7 @@ def compile_crosscheck_names(element: SaColumn, @compiles(CrosscheckNames, 'sqlite') # type: ignore[no-untyped-call, misc] -def compile_sqlite_crosscheck_names(element: SaColumn, +def compile_sqlite_crosscheck_names(element: CrosscheckNames, compiler: 'sa.Compiled', **kw: Any) -> str: arg1, arg2 = list(element.clauses) return "EXISTS(SELECT *"\ @@ -191,12 +191,12 @@ class JsonArrayEach(sa.sql.functions.GenericFunction[Any]): @compiles(JsonArrayEach) # type: ignore[no-untyped-call, misc] -def default_json_array_each(element: SaColumn, compiler: 'sa.Compiled', **kw: Any) -> str: +def default_json_array_each(element: JsonArrayEach, compiler: 'sa.Compiled', **kw: Any) -> str: return "json_array_elements(%s)" % compiler.process(element.clauses, **kw) @compiles(JsonArrayEach, 'sqlite') # type: ignore[no-untyped-call, misc] -def sqlite_json_array_each(element: SaColumn, compiler: 'sa.Compiled', **kw: Any) -> str: +def sqlite_json_array_each(element: JsonArrayEach, compiler: 'sa.Compiled', **kw: Any) -> str: return "json_each(%s)" % compiler.process(element.clauses, **kw) @@ -208,5 +208,5 @@ class Greatest(sa.sql.functions.GenericFunction[Any]): @compiles(Greatest, 'sqlite') # type: ignore[no-untyped-call, misc] -def sqlite_greatest(element: SaColumn, compiler: 'sa.Compiled', **kw: Any) -> str: +def sqlite_greatest(element: Greatest, compiler: 'sa.Compiled', **kw: Any) -> str: return "max(%s)" % compiler.process(element.clauses, **kw) diff --git a/nominatim/db/sqlalchemy_types/geometry.py b/nominatim/db/sqlalchemy_types/geometry.py index 4520fc8e..0731b0b7 100644 --- a/nominatim/db/sqlalchemy_types/geometry.py +++ b/nominatim/db/sqlalchemy_types/geometry.py @@ -28,7 +28,7 @@ class Geometry_DistanceSpheroid(sa.sql.expression.FunctionElement[float]): @compiles(Geometry_DistanceSpheroid) # type: ignore[no-untyped-call, misc] -def _default_distance_spheroid(element: SaColumn, +def _default_distance_spheroid(element: Geometry_DistanceSpheroid, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_DistanceSpheroid(%s,"\ " 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]')"\ @@ -36,7 +36,7 @@ def _default_distance_spheroid(element: SaColumn, @compiles(Geometry_DistanceSpheroid, 'sqlite') # type: ignore[no-untyped-call, misc] -def _spatialite_distance_spheroid(element: SaColumn, +def _spatialite_distance_spheroid(element: Geometry_DistanceSpheroid, compiler: 'sa.Compiled', **kw: Any) -> str: return "COALESCE(Distance(%s, true), 0.0)" % compiler.process(element.clauses, **kw) @@ -49,14 +49,14 @@ class Geometry_IsLineLike(sa.sql.expression.FunctionElement[Any]): @compiles(Geometry_IsLineLike) # type: ignore[no-untyped-call, misc] -def _default_is_line_like(element: SaColumn, +def _default_is_line_like(element: Geometry_IsLineLike, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_GeometryType(%s) IN ('ST_LineString', 'ST_MultiLineString')" % \ compiler.process(element.clauses, **kw) @compiles(Geometry_IsLineLike, 'sqlite') # type: ignore[no-untyped-call, misc] -def _sqlite_is_line_like(element: SaColumn, +def _sqlite_is_line_like(element: Geometry_IsLineLike, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_GeometryType(%s) IN ('LINESTRING', 'MULTILINESTRING')" % \ compiler.process(element.clauses, **kw) @@ -70,14 +70,14 @@ class Geometry_IsAreaLike(sa.sql.expression.FunctionElement[Any]): @compiles(Geometry_IsAreaLike) # type: ignore[no-untyped-call, misc] -def _default_is_area_like(element: SaColumn, +def _default_is_area_like(element: Geometry_IsAreaLike, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_GeometryType(%s) IN ('ST_Polygon', 'ST_MultiPolygon')" % \ compiler.process(element.clauses, **kw) @compiles(Geometry_IsAreaLike, 'sqlite') # type: ignore[no-untyped-call, misc] -def _sqlite_is_area_like(element: SaColumn, +def _sqlite_is_area_like(element: Geometry_IsAreaLike, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_GeometryType(%s) IN ('POLYGON', 'MULTIPOLYGON')" % \ compiler.process(element.clauses, **kw) @@ -91,14 +91,14 @@ class Geometry_IntersectsBbox(sa.sql.expression.FunctionElement[Any]): @compiles(Geometry_IntersectsBbox) # type: ignore[no-untyped-call, misc] -def _default_intersects(element: SaColumn, +def _default_intersects(element: Geometry_IntersectsBbox, compiler: 'sa.Compiled', **kw: Any) -> str: arg1, arg2 = list(element.clauses) return "%s && %s" % (compiler.process(arg1, **kw), compiler.process(arg2, **kw)) @compiles(Geometry_IntersectsBbox, 'sqlite') # type: ignore[no-untyped-call, misc] -def _sqlite_intersects(element: SaColumn, +def _sqlite_intersects(element: Geometry_IntersectsBbox, compiler: 'sa.Compiled', **kw: Any) -> str: return "MbrIntersects(%s) = 1" % compiler.process(element.clauses, **kw) @@ -114,14 +114,14 @@ class Geometry_ColumnIntersectsBbox(sa.sql.expression.FunctionElement[Any]): @compiles(Geometry_ColumnIntersectsBbox) # type: ignore[no-untyped-call, misc] -def default_intersects_column(element: SaColumn, +def default_intersects_column(element: Geometry_ColumnIntersectsBbox, compiler: 'sa.Compiled', **kw: Any) -> str: arg1, arg2 = list(element.clauses) return "%s && %s" % (compiler.process(arg1, **kw), compiler.process(arg2, **kw)) @compiles(Geometry_ColumnIntersectsBbox, 'sqlite') # type: ignore[no-untyped-call, misc] -def spatialite_intersects_column(element: SaColumn, +def spatialite_intersects_column(element: Geometry_ColumnIntersectsBbox, compiler: 'sa.Compiled', **kw: Any) -> str: arg1, arg2 = list(element.clauses) return "MbrIntersects(%s, %s) = 1 and "\ @@ -145,12 +145,12 @@ class Geometry_ColumnDWithin(sa.sql.expression.FunctionElement[Any]): @compiles(Geometry_ColumnDWithin) # type: ignore[no-untyped-call, misc] -def default_dwithin_column(element: SaColumn, +def default_dwithin_column(element: Geometry_ColumnDWithin, compiler: 'sa.Compiled', **kw: Any) -> str: return "ST_DWithin(%s)" % compiler.process(element.clauses, **kw) @compiles(Geometry_ColumnDWithin, 'sqlite') # type: ignore[no-untyped-call, misc] -def spatialite_dwithin_column(element: SaColumn, +def spatialite_dwithin_column(element: Geometry_ColumnDWithin, compiler: 'sa.Compiled', **kw: Any) -> str: geom1, geom2, dist = list(element.clauses) return "ST_Distance(%s, %s) < %s and "\ -- 2.39.5