Skip to content

Commit

Permalink
fix: handle unhashable schema types like lists to prevent unreachable…
Browse files Browse the repository at this point in the history
… exception
  • Loading branch information
philippwaller committed Jan 12, 2025
1 parent ca8e63b commit bb2dd9a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum

import pytest
import voluptuous as vol

from voluptuous_serialize import UNSUPPORTED, convert
Expand Down Expand Up @@ -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))
4 changes: 2 additions & 2 deletions voluptuous_serialize/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)):
Expand Down

0 comments on commit bb2dd9a

Please sign in to comment.