Skip to content

Commit

Permalink
Test a field definition edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Sep 20, 2020
1 parent bbc3615 commit 903b6c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 9 additions & 8 deletions odmantic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ def __new__( # noqa C901
namespace: Dict[str, Any],
**kwargs: Any,
) -> "BaseModelMetaclass":
if (namespace.get("__module__"), namespace.get("__qualname__")) != (
"odmantic.model",
"Model",
):
if namespace.get("__module__") != "odmantic.model" and namespace.get(
"__qualname__"
) not in ("Model", "EmbeddedModel"):
annotations = resolve_annotations(
namespace.get("__annotations__", {}), namespace.get("__module__")
)
Expand Down Expand Up @@ -124,13 +123,15 @@ def __new__( # noqa C901

# Validate fields
for (field_name, field_type) in annotations.items():
if not is_valid_odm_field(field_name) or lenient_issubclass(
field_type, UNTOUCHED_TYPES
value = namespace.get(field_name, Undefined)

if (
not is_valid_odm_field(field_name)
or lenient_issubclass(field_type, UNTOUCHED_TYPES)
or isinstance(value, UNTOUCHED_TYPES)
):
continue

value = namespace.get(field_name, Undefined)

if isinstance(value, PDFieldInfo):
raise TypeError(
"please use odmantic.Field instead of pydantic.Field"
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_model_definition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional
from typing import Any, Callable, Optional

import pytest
from pydantic import Field as PDField
Expand Down Expand Up @@ -285,3 +285,13 @@ class M(Model):
field: E = Field(primary_field=True)

assert M(field=E(f=1)).doc() == {"_id": {"f": 1}}


def test_untouched_types_function():
def id_str(self) -> str:
return str(self.id)

class M(Model):
id_: Callable = id_str

assert "id_" not in M.__odm_fields__.keys()

0 comments on commit 903b6c6

Please sign in to comment.