From bb2dd9a1934feff203b69fa97154874ebdd8334e Mon Sep 17 00:00:00 2001 From: Philipp Waller <1090452+philippwaller@users.noreply.github.com> Date: Mon, 13 Jan 2025 00:15:25 +0100 Subject: [PATCH] fix: handle unhashable schema types like lists to prevent unreachable exception --- tests/test_lib.py | 13 +++++-------- voluptuous_serialize/__init__.py | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/test_lib.py b/tests/test_lib.py index e6267fa..9d2317c 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -1,5 +1,6 @@ from enum import Enum +import pytest import voluptuous as vol from voluptuous_serialize import UNSUPPORTED, convert @@ -216,11 +217,7 @@ class TestEnum(Enum): } == convert(vol.Schema(vol.Coerce(TestEnum))) -def test_invalid_schema(): - # check if an exception is raised when an invalid schema is passed - try: - convert(vol.Schema(None)) - except Exception as e: - assert str(e) == "Unable to convert schema: None" - else: - assert False, "Expected an exception to be raised" +@pytest.mark.parametrize("invalid_schema", [None, []]) +def test_invalid_schema(invalid_schema): + with pytest.raises(ValueError): + convert(vol.Schema(invalid_schema)) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 72bd09c..40e5ed6 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -1,6 +1,6 @@ """Module to convert voluptuous schemas to dictionaries.""" -from collections.abc import Mapping +from collections.abc import Hashable, Mapping from enum import Enum import voluptuous as vol @@ -112,7 +112,7 @@ def convert(schema, *, custom_serializer=None): if isinstance(schema, vol.Coerce): schema = schema.type - if schema in TYPES_MAP: + if isinstance(schema, Hashable) and schema in TYPES_MAP: return {"type": TYPES_MAP[schema]} if isinstance(schema, (str, int, float, bool)):