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

Literal types are immutable #345

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions odmantic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_model_type_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
),
Expand Down