From 17a65d82bb002e482f9aa26d29f9a4ab8e3aeb7c Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sun, 2 Jul 2023 23:05:15 +0200 Subject: [PATCH] make types compatible with older Python versions --- nominatim/api/logging.py | 4 ++++ nominatim/db/sqlalchemy_types.py | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/nominatim/api/logging.py b/nominatim/api/logging.py index 3160ede8..37855790 100644 --- a/nominatim/api/logging.py +++ b/nominatim/api/logging.py @@ -12,6 +12,7 @@ from contextvars import ContextVar import datetime as dt import textwrap import io +import re import sqlalchemy as sa from sqlalchemy.ext.asyncio import AsyncConnection @@ -103,6 +104,9 @@ class BaseLogger: except TypeError: return sqlstr + # Fixes an odd issue with Python 3.7 where percentages are not + # quoted correctly. + sqlstr = re.sub(r'%(?!\()', '%%', sqlstr) return sqlstr % params diff --git a/nominatim/db/sqlalchemy_types.py b/nominatim/db/sqlalchemy_types.py index f5ec82bd..ed4aef1f 100644 --- a/nominatim/db/sqlalchemy_types.py +++ b/nominatim/db/sqlalchemy_types.py @@ -7,7 +7,8 @@ """ Custom types for SQLAlchemy. """ -from typing import Callable, Any +from typing import Callable, Any, cast +import sys import sqlalchemy as sa from sqlalchemy import types @@ -16,7 +17,7 @@ from nominatim.typing import SaColumn, SaBind #pylint: disable=all -class Geometry(types.UserDefinedType[Any]): +class Geometry(types.UserDefinedType): # type: ignore[type-arg] """ Simplified type decorator for PostGIS geometry. This type only supports geometries in 4326 projection. """ @@ -35,7 +36,7 @@ class Geometry(types.UserDefinedType[Any]): if isinstance(value, str): return 'SRID=4326;' + value - return 'SRID=4326;' + value.to_wkt() + return 'SRID=4326;' + cast(str, value.to_wkt()) return process @@ -50,7 +51,7 @@ class Geometry(types.UserDefinedType[Any]): return sa.func.ST_GeomFromText(bindvalue, type_=self) - class comparator_factory(types.UserDefinedType.Comparator): + class comparator_factory(types.UserDefinedType.Comparator): # type: ignore[type-arg] def intersects(self, other: SaColumn) -> 'sa.Operators': return self.op('&&')(other) -- 2.39.5