From 49cb481f3dd776882ad7c205f835211b4b3fcab4 Mon Sep 17 00:00:00 2001 From: David Smith Date: Thu, 7 Nov 2024 20:37:16 +0000 Subject: [PATCH] Added message to TypeError exceptions in GEOS WKTReader and WKBReader. --- django/contrib/gis/geos/prototypes/io.py | 6 ++++-- tests/gis_tests/geos_tests/test_io.py | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/django/contrib/gis/geos/prototypes/io.py b/django/contrib/gis/geos/prototypes/io.py index efe9ec159f..def976329d 100644 --- a/django/contrib/gis/geos/prototypes/io.py +++ b/django/contrib/gis/geos/prototypes/io.py @@ -156,7 +156,7 @@ class _WKTReader(IOBase): def read(self, wkt): if not isinstance(wkt, (bytes, str)): - raise TypeError + raise TypeError(f"'wkt' must be bytes or str (got {wkt!r} instead).") return wkt_reader_read(self.ptr, force_bytes(wkt)) @@ -176,7 +176,9 @@ class _WKBReader(IOBase): wkb_s = wkb.encode() return wkb_reader_read_hex(self.ptr, wkb_s, len(wkb_s)) else: - raise TypeError + raise TypeError( + f"'wkb' must be bytes, str or memoryview (got {wkb!r} instead)." + ) def default_trim_value(): diff --git a/tests/gis_tests/geos_tests/test_io.py b/tests/gis_tests/geos_tests/test_io.py index 35cf0c11ad..14646ce385 100644 --- a/tests/gis_tests/geos_tests/test_io.py +++ b/tests/gis_tests/geos_tests/test_io.py @@ -29,15 +29,23 @@ class GEOSIOTest(SimpleTestCase): self.assertEqual(ref, geom) # Should only accept string objects. - with self.assertRaises(TypeError): - wkt_r.read(1) - with self.assertRaises(TypeError): - wkt_r.read(memoryview(b"foo")) + bad_input = (1, 5.23, None, False, memoryview(b"foo")) + msg = "'wkt' must be bytes or str (got {} instead)." + for bad_wkt in bad_input: + with ( + self.subTest(bad_wkt=bad_wkt), + self.assertRaisesMessage(TypeError, msg.format(bad_wkt)), + ): + wkt_r.read(bad_wkt) def test02_wktwriter(self): # Creating a WKTWriter instance, testing its ptr property. wkt_w = WKTWriter() - with self.assertRaises(TypeError): + msg = ( + "Incompatible pointer type: " + "." + ) + with self.assertRaisesMessage(TypeError, msg): wkt_w.ptr = WKTReader.ptr_type() ref = GEOSGeometry("POINT (5 23)") @@ -72,8 +80,12 @@ class GEOSIOTest(SimpleTestCase): self.assertEqual(ref, geom) bad_input = (1, 5.23, None, False) + msg = "'wkb' must be bytes, str or memoryview (got {} instead)." for bad_wkb in bad_input: - with self.assertRaises(TypeError): + with ( + self.subTest(bad_wkb=bad_wkb), + self.assertRaisesMessage(TypeError, msg.format(bad_wkb)), + ): wkb_r.read(bad_wkb) def test04_wkbwriter(self):