Skip to content

Commit

Permalink
fix: load generic dict scalar fields
Browse files Browse the repository at this point in the history
  • Loading branch information
thearchitector committed Sep 17, 2024
1 parent c16e83b commit 7a56f75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion indico/types/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import json
from datetime import datetime
from typing import TYPE_CHECKING, Any, List, cast
from typing import TYPE_CHECKING, Any, List, cast, get_origin

from indico.types.utils import cc_to_snake

Expand Down Expand Up @@ -38,6 +38,7 @@ def valid_type(v: "Any") -> bool:
return (
(inspect.isclass(v) and issubclass(v, BaseType))
or v in [str, int, float, bool, JSONType, datetime]
or get_origin(v) is dict
or valid_type(list_subtype(v))
)

Expand Down
11 changes: 10 additions & 1 deletion tests/unit/types/test_base_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import List
from typing import Dict, List

from indico.types.base import BaseType, JSONType

Expand Down Expand Up @@ -97,3 +97,12 @@ class A(BaseType):
x = A(**{"createdAt": "1590169591.582852"})

assert x.created_at == datetime.fromtimestamp(1590169591.582852)


def test_generic_dict_field():
class A(BaseType):
meta: Dict[str, str]

x = A(meta={"foo": "bar"})

assert x.meta == {"foo": "bar"}

0 comments on commit 7a56f75

Please sign in to comment.