From 49e0d83d5d8e3008373ae99ae3f37788f4b9a80a Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sat, 1 Jul 2023 18:02:46 +0200 Subject: [PATCH] fix linting issues --- nominatim/api/logging.py | 10 ++++++---- nominatim/api/reverse.py | 10 +++++----- nominatim/api/search/db_searches.py | 20 ++++++++++---------- nominatim/api/types.py | 8 +++----- nominatim/db/sqlalchemy_types.py | 10 ++++++---- nominatim/typing.py | 1 + 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/nominatim/api/logging.py b/nominatim/api/logging.py index 202d7de9..3160ede8 100644 --- a/nominatim/api/logging.py +++ b/nominatim/api/logging.py @@ -80,7 +80,8 @@ class BaseLogger: """ def format_sql(self, conn: AsyncConnection, statement: 'sa.Executable', - extra_params: Union[Mapping[str, Any], Sequence[Mapping[str, Any]], None]) -> str: + extra_params: Union[Mapping[str, Any], + Sequence[Mapping[str, Any]], None]) -> str: """ Return the comiled version of the statement. """ compiled = cast('sa.ClauseElement', statement).compile(conn.sync_engine) @@ -95,13 +96,14 @@ class BaseLogger: sqlstr = str(compiled) - if '%s' in sqlstr: + if sa.__version__.startswith('1'): try: - return sqlstr % tuple((repr(compiled.params[name]) for name in compiled.positiontup)) + return sqlstr % tuple((repr(params.get(name, None)) + for name in compiled.positiontup)) # type: ignore except TypeError: return sqlstr - return str(compiled) % params + return sqlstr % params class HTMLLogger(BaseLogger): diff --git a/nominatim/api/reverse.py b/nominatim/api/reverse.py index 62239a54..00605d45 100644 --- a/nominatim/api/reverse.py +++ b/nominatim/api/reverse.py @@ -7,11 +7,11 @@ """ Implementation of reverse geocoding. """ -from typing import Optional, List, Callable, Type, Tuple +from typing import Optional, List, Callable, Type, Tuple, Dict, Any import sqlalchemy as sa -from nominatim.typing import SaColumn, SaSelect, SaFromClause, SaLabel, SaRow +from nominatim.typing import SaColumn, SaSelect, SaFromClause, SaLabel, SaRow, SaBind from nominatim.api.connection import SearchConnection import nominatim.api.results as nres from nominatim.api.logging import log @@ -24,8 +24,8 @@ from nominatim.db.sqlalchemy_types import Geometry RowFunc = Callable[[Optional[SaRow], Type[nres.ReverseResult]], Optional[nres.ReverseResult]] -WKT_PARAM = sa.bindparam('wkt', type_=Geometry) -MAX_RANK_PARAM = sa.bindparam('max_rank') +WKT_PARAM: SaBind = sa.bindparam('wkt', type_=Geometry) +MAX_RANK_PARAM: SaBind = sa.bindparam('max_rank') def _select_from_placex(t: SaFromClause, use_wkt: bool = True) -> SaSelect: """ Create a select statement with the columns relevant for reverse @@ -93,7 +93,7 @@ class ReverseGeocoder: self.conn = conn self.params = params - self.bind_params = {'max_rank': params.max_rank} + self.bind_params: Dict[str, Any] = {'max_rank': params.max_rank} @property diff --git a/nominatim/api/search/db_searches.py b/nominatim/api/search/db_searches.py index fc3f9e09..cea19c85 100644 --- a/nominatim/api/search/db_searches.py +++ b/nominatim/api/search/db_searches.py @@ -14,7 +14,7 @@ import sqlalchemy as sa from sqlalchemy.dialects.postgresql import ARRAY, array_agg from nominatim.typing import SaFromClause, SaScalarSelect, SaColumn, \ - SaExpression, SaSelect, SaRow + SaExpression, SaSelect, SaRow, SaBind from nominatim.api.connection import SearchConnection from nominatim.api.types import SearchDetails, DataLayer, GeometryFormat, Bbox import nominatim.api.results as nres @@ -39,15 +39,15 @@ def _details_to_bind_params(details: SearchDetails) -> Dict[str, Any]: 'countries': details.countries} -LIMIT_PARAM = sa.bindparam('limit') -MIN_RANK_PARAM = sa.bindparam('min_rank') -MAX_RANK_PARAM = sa.bindparam('max_rank') -VIEWBOX_PARAM = sa.bindparam('viewbox', type_=Geometry) -VIEWBOX2_PARAM = sa.bindparam('viewbox2', type_=Geometry) -NEAR_PARAM = sa.bindparam('near', type_=Geometry) -NEAR_RADIUS_PARAM = sa.bindparam('near_radius') -EXCLUDED_PARAM = sa.bindparam('excluded') -COUNTRIES_PARAM = sa.bindparam('countries') +LIMIT_PARAM: SaBind = sa.bindparam('limit') +MIN_RANK_PARAM: SaBind = sa.bindparam('min_rank') +MAX_RANK_PARAM: SaBind = sa.bindparam('max_rank') +VIEWBOX_PARAM: SaBind = sa.bindparam('viewbox', type_=Geometry) +VIEWBOX2_PARAM: SaBind = sa.bindparam('viewbox2', type_=Geometry) +NEAR_PARAM: SaBind = sa.bindparam('near', type_=Geometry) +NEAR_RADIUS_PARAM: SaBind = sa.bindparam('near_radius') +EXCLUDED_PARAM: SaBind = sa.bindparam('excluded') +COUNTRIES_PARAM: SaBind = sa.bindparam('countries') def _select_placex(t: SaFromClause) -> SaSelect: return sa.select(t.c.place_id, t.c.osm_type, t.c.osm_id, t.c.name, diff --git a/nominatim/api/types.py b/nominatim/api/types.py index 60495dd0..43e83c1f 100644 --- a/nominatim/api/types.py +++ b/nominatim/api/types.py @@ -16,8 +16,6 @@ import math from struct import unpack from binascii import unhexlify -import sqlalchemy as sa - from nominatim.errors import UsageError # pylint: disable=no-member,too-many-boolean-expressions,too-many-instance-attributes @@ -192,7 +190,8 @@ class Bbox: """ Return the WKT representation of the Bbox. This is a simple polygon with four points. """ - return 'POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))'.format(*self.coords) + return 'POLYGON(({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))'\ + .format(*self.coords) # pylint: disable=consider-using-f-string @staticmethod @@ -445,6 +444,7 @@ class SearchDetails(LookupDetails): """ Restrict search to places with one of the given class/type categories. An empty list (the default) will disable this filter. """ + viewbox_x2: Optional[Bbox] = None def __post_init__(self) -> None: if self.viewbox is not None: @@ -452,8 +452,6 @@ class SearchDetails(LookupDetails): yext = (self.viewbox.maxlat - self.viewbox.minlat)/2 self.viewbox_x2 = Bbox(self.viewbox.minlon - xext, self.viewbox.minlat - yext, self.viewbox.maxlon + xext, self.viewbox.maxlat + yext) - else: - self.viewbox_x2 = None def restrict_min_max_rank(self, new_min: int, new_max: int) -> None: diff --git a/nominatim/db/sqlalchemy_types.py b/nominatim/db/sqlalchemy_types.py index c54d339e..f5ec82bd 100644 --- a/nominatim/db/sqlalchemy_types.py +++ b/nominatim/db/sqlalchemy_types.py @@ -10,9 +10,11 @@ Custom types for SQLAlchemy. from typing import Callable, Any import sqlalchemy as sa -import sqlalchemy.types as types +from sqlalchemy import types -from nominatim.typing import SaColumn +from nominatim.typing import SaColumn, SaBind + +#pylint: disable=all class Geometry(types.UserDefinedType[Any]): """ Simplified type decorator for PostGIS geometry. This type @@ -44,13 +46,13 @@ class Geometry(types.UserDefinedType[Any]): return process - def bind_expression(self, bindvalue: 'sa.BindParameter[Any]') -> SaColumn: + def bind_expression(self, bindvalue: SaBind) -> SaColumn: return sa.func.ST_GeomFromText(bindvalue, type_=self) class comparator_factory(types.UserDefinedType.Comparator): - def intersects(self, other: SaColumn) -> SaColumn: + def intersects(self, other: SaColumn) -> 'sa.Operators': return self.op('&&')(other) def is_line_like(self) -> SaColumn: diff --git a/nominatim/typing.py b/nominatim/typing.py index d988fe04..ebb5e1e9 100644 --- a/nominatim/typing.py +++ b/nominatim/typing.py @@ -70,3 +70,4 @@ SaExpression: TypeAlias = 'sa.ColumnElement[bool]' SaLabel: TypeAlias = 'sa.Label[Any]' SaFromClause: TypeAlias = 'sa.FromClause' SaSelectable: TypeAlias = 'sa.Selectable' +SaBind: TypeAlias = 'sa.BindParameter[Any]' -- 2.39.5