From e99f26b90b404300e2e132965547d2bc448674c4 Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Mon, 24 Mar 2025 11:32:41 +0000 Subject: [PATCH] fix(shapely): maintain Shapely < 2 compatibility --- psycopg/psycopg/types/shapely.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/psycopg/psycopg/types/shapely.py b/psycopg/psycopg/types/shapely.py index bce1c2490..b8e287b11 100644 --- a/psycopg/psycopg/types/shapely.py +++ b/psycopg/psycopg/types/shapely.py @@ -12,9 +12,7 @@ from .._compat import cache from .._typeinfo import TypeInfo try: - from shapely import from_wkb, to_wkb from shapely.geometry.base import BaseGeometry - except ImportError: raise ImportError( "The module psycopg.types.shapely requires the package 'Shapely'" @@ -22,29 +20,38 @@ except ImportError: ) +try: + from shapely import from_wkb as from_wkb_compat + from shapely import to_wkb as to_wkb_compat +except ImportError: + # Shapely<2 compatibility + from shapely.wkb import dumps as to_wkb_compat # type: ignore[no-redef] + from shapely.wkb import loads as from_wkb_compat # type: ignore[no-redef] + + class GeometryBinaryLoader(Loader): format = Format.BINARY def load(self, data: Buffer) -> BaseGeometry: - return from_wkb(bytes(data)) + return from_wkb_compat(bytes(data)) class GeometryLoader(Loader): def load(self, data: Buffer) -> BaseGeometry: # it's a hex string in binary - return from_wkb(bytes(data)) + return from_wkb_compat(bytes(data)) class BaseGeometryBinaryDumper(Dumper): format = Format.BINARY def dump(self, obj: BaseGeometry) -> Buffer | None: - return to_wkb(obj, include_srid=True) + return to_wkb_compat(obj, include_srid=True) class BaseGeometryDumper(Dumper): def dump(self, obj: BaseGeometry) -> Buffer | None: - return to_wkb(obj, True, include_srid=True).encode() + return to_wkb_compat(obj, True, include_srid=True).encode() def register_shapely(info: TypeInfo, context: AdaptContext | None = None) -> None: -- 2.47.2