]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Raise a friendly exception if None is passed to register_*()
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 21 Sep 2021 15:55:52 +0000 (16:55 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 21 Sep 2021 17:01:32 +0000 (18:01 +0100)
The most likely cause is fetching info and the object is not found.
Suggest that that's what happened instead of throwing an AttributeError
or anything else random.

psycopg/psycopg/types/composite.py
psycopg/psycopg/types/hstore.py
psycopg/psycopg/types/range.py
tests/types/test_composite.py
tests/types/test_hstore.py
tests/types/test_range.py

index 7a754dc40d34f148748a3421f49519305c2b227a..c8011187547208ba1ac4ea0237f487a96c6594cf 100644 (file)
@@ -217,6 +217,13 @@ def register_composite(
         the composite into a Python object.
     """
 
+    # 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 requested composite available?"
+        )
+
     # Register arrays and type info
     info.register(context)
 
index f39bec47aae50e72fe91260ff038557182f8511a..e45b9414f131c584dbe0dd9592265356c0baac08 100644 (file)
@@ -107,6 +107,10 @@ def register_hstore(
     :param context: The context where to register the adapters. If `!None`,
         register it globally.
     """
+    # 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 'hstore' extension loaded?")
 
     # Register arrays and type info
     info.register(context)
index 6c353b5e5e26b085004f7ba2b1632ad495f1b512..59fa04dd35aa91dc341d723ff0d0415247790cb4 100644 (file)
@@ -442,6 +442,10 @@ def register_range(
     Register loaders so that loading data of this type will result in a `Range`
     with bounds parsed as the right subtype.
     """
+    # 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 requested range available?")
 
     # Register arrays and type info
     info.register(context)
index cfc2c91e3202ca017c00070f0ad41bab80d7dbd0..cb1125754dd2b2e45545ead0711e9b3e321a4be7 100644 (file)
@@ -327,3 +327,8 @@ def test_callable_dumper_not_registered(conn, testcomp):
     # but the loader is registered
     cur = conn.execute("select '(foo,42,3.14)'::testcomp")
     assert cur.fetchone()[0] == ("foo", 42, 3.14, 3.14)
+
+
+def test_no_info_error(conn):
+    with pytest.raises(TypeError, match="composite"):
+        register_composite(None, conn)
index 5c951548f3cf5ddacbb278bbb83f696b292cecac..337d12a326ed708323d537af1a44fa9a107f5b69 100644 (file)
@@ -97,3 +97,8 @@ def test_roundtrip_array(hstore, conn):
     register_hstore(TypeInfo.fetch(conn, "hstore"), conn)
     samp1 = conn.execute("select %s", (samp,)).fetchone()[0]
     assert samp1 == samp
+
+
+def test_no_info_error(conn):
+    with pytest.raises(TypeError, match="hstore.*extension"):
+        register_hstore(None, conn)
index 9ed018c15bcc40a0a0943fc970b5a3aad161774a..2860cac8f6f1472e925b1f58afec74176d2de852 100644 (file)
@@ -667,3 +667,8 @@ class TestRangeObject:
         expected = "[2010-01-01 00:00:00-05:00, 2011-01-01 00:00:00-05:00)"
         result = str(r)
         assert result == expected
+
+
+def test_no_info_error(conn):
+    with pytest.raises(TypeError, match="range"):
+        register_range(None, conn)