From: Daniele Varrazzo Date: Tue, 21 Sep 2021 15:54:14 +0000 (+0100) Subject: Report a friendly message if the geometry info is not available X-Git-Tag: 3.0~71^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e4134554da74795a9ac336c7cfae188db278a98b;p=thirdparty%2Fpsycopg.git Report a friendly message if the geometry info is not available --- diff --git a/psycopg/psycopg/types/shapely.py b/psycopg/psycopg/types/shapely.py index 566c65fa6..2686a6e74 100644 --- a/psycopg/psycopg/types/shapely.py +++ b/psycopg/psycopg/types/shapely.py @@ -74,6 +74,11 @@ def register_shapely( """ + # A friendly error warning instead of an AttributeError in case fetch() + # failed and it wasn't noticed. + if not info: + raise TypeError("no info passed. Is the 'postgis' extension loaded?") + info.register(context) adapters = context.adapters if context else postgres.adapters # Generate and register the text and binary dumper diff --git a/tests/types/test_shapely.py b/tests/types/test_shapely.py index fd4447b77..327fa9c5d 100644 --- a/tests/types/test_shapely.py +++ b/tests/types/test_shapely.py @@ -77,6 +77,13 @@ def test_no_adapter(conn): conn.execute("SELECT pg_typeof(%s)", [point]).fetchone()[0] +def test_no_info_error(conn): + from psycopg.types.shapely import register_shapely + + with pytest.raises(TypeError, match="postgis.*extension"): + register_shapely(None, conn) + + def test_with_adapter(shapely_conn): SAMPLE_POINT = Point(1.2, 3.4) SAMPLE_POLYGON = Polygon([(0, 0), (1, 1), (1, 0)])