diff --git a/odmantic/model.py b/odmantic/model.py index f387f937..e0128200 100644 --- a/odmantic/model.py +++ b/odmantic/model.py @@ -138,6 +138,8 @@ def find_duplicate_key(fields: Iterable[ODMBaseField]) -> Optional[str]: def is_type_mutable(type_: Type) -> bool: type_origin: Optional[Type] = getattr(type_, "__origin__", None) if type_origin is not None: + if type_origin is Literal: + return False type_args: Tuple[Type, ...] = getattr(type_, "__args__", ()) for type_arg in type_args: if type_arg is ...: # Handle tuple definition diff --git a/tests/unit/test_model_type_validation.py b/tests/unit/test_model_type_validation.py index c8a5eb0b..c5d23f0a 100644 --- a/tests/unit/test_model_type_validation.py +++ b/tests/unit/test_model_type_validation.py @@ -24,6 +24,7 @@ Regex, ) from odmantic.model import EmbeddedModel, Model, is_type_mutable, validate_type +from odmantic.typing import Literal @pytest.mark.parametrize("base, replacement", _BSON_SUBSTITUTED_FIELDS.items()) @@ -83,6 +84,7 @@ class DummyModel(Model): Tuple[int, str, bool], Tuple[int, ...], FrozenSet[int], + Literal["a", "b", "c"], Union[FrozenSet[int], Tuple[int, str]], DummyModel, ),