Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeError in convert function when schema is not a class #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 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 @@ -214,3 +215,9 @@ class TestEnum(Enum):
(2, 2),
],
} == convert(vol.Schema(vol.Coerce(TestEnum)))


@pytest.mark.parametrize("invalid_schema", [None, []])
def test_invalid_schema(invalid_schema):
with pytest.raises(ValueError):
convert(vol.Schema(invalid_schema))
6 changes: 3 additions & 3 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,13 +112,13 @@ 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)):
return {"type": "constant", "value": schema}

if issubclass(schema, Enum):
if isinstance(schema, type) and issubclass(schema, Enum):
return {
"type": "select",
"options": [(item.value, item.value) for item in schema],
Expand Down