Skip to content

Commit b0ae3ec

Browse files
committed
Process empty dict as Dict type
1 parent 0113405 commit b0ae3ec

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- Features
44
- Models layer
55
- [X] Data variant converting
6-
- [ ] Change empty dict to actual dict type, not empty models
6+
- [X] Change empty dict to actual dict type, not empty models
77
- [X] Data variant merging
88
- [X] Create and register models
99
- [X] Merge meta-models and extract common ones

json_to_models/generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def _detect_type(self, value, convert_dict=True) -> MetaData:
7777

7878
# Dict should be processed as another model if convert_dict is enabled
7979
elif isinstance(value, dict):
80+
if not value:
81+
return DDict(Unknown)
8082
for reg in self.dict_keys_regex:
8183
if all(map(reg.match, value.keys())):
8284
convert_dict = False
@@ -91,10 +93,8 @@ def _detect_type(self, value, convert_dict=True) -> MetaData:
9193
if len(union.types) == 1:
9294
return DDict(*union.types)
9395
return DDict(union)
94-
elif types:
95-
return DDict(*types)
9696
else:
97-
return DDict(Unknown)
97+
return DDict(*types)
9898

9999
# null interpreted as is and will be processed later on Union merge stage
100100
elif value is None:

json_to_models/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import OrderedDict, defaultdict
22
from itertools import chain, combinations
3-
from typing import Dict, List, Set, Tuple
3+
from typing import Dict, Iterable, List, Set, Tuple
44

55
from ordered_set import OrderedSet
66

@@ -147,7 +147,7 @@ def merge_models(self, generator, strict=False) -> List[Tuple[ModelMeta, Set[Mod
147147
models2merge[model_b].add(model_a)
148148

149149
# Groups of models to merge
150-
groups: List[Set[ModelMeta]] = [{model, *models} for model, models in models2merge.items()]
150+
groups: Iterable[Set[ModelMeta]] = [{model, *models} for model, models in models2merge.items()]
151151
# Make groups non-overlapping.
152152
# This is not optimal algorithm but it works and we probably will not have thousands of models here.
153153
flag = True

test/test_code_generation/test_models_code_generator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import pytest
44

5-
from json_to_models.dynamic_typing import (AbsoluteModelRef, DList, DOptional, IntString, ModelMeta, ModelPtr,
6-
compile_imports)
5+
from json_to_models.dynamic_typing import (AbsoluteModelRef, DDict, DList, DOptional, IntString, ModelMeta, ModelPtr,
6+
Unknown, compile_imports)
77
from json_to_models.models import indent, sort_fields
88
from json_to_models.models.base import GenericModelCodeGenerator, generate_code
99

@@ -101,7 +101,8 @@ class Test:
101101
"model": ("Test", {
102102
"foo": int,
103103
"baz": DOptional(DList(DList(str))),
104-
"bar": IntString
104+
"bar": IntString,
105+
"d": DDict(Unknown)
105106
}),
106107
"fields_data": {
107108
"foo": {
@@ -115,25 +116,31 @@ class Test:
115116
"bar": {
116117
"name": "bar",
117118
"type": "IntString"
119+
},
120+
"d": {
121+
"name": "d",
122+
"type": "Dict[str, Any]"
118123
}
119124
},
120125
"fields": {
121126
"imports": "from json_to_models.dynamic_typing import IntString\n"
122-
"from typing import List, Optional",
127+
"from typing import Any, Dict, List, Optional",
123128
"fields": [
124129
"foo: int",
125130
"bar: IntString",
131+
"d: Dict[str, Any]",
126132
"baz: Optional[List[List[str]]]",
127133
]
128134
},
129135
"generated": trim("""
130136
from json_to_models.dynamic_typing import IntString
131-
from typing import List, Optional
137+
from typing import Any, Dict, List, Optional
132138
133139
134140
class Test:
135141
foo: int
136142
bar: IntString
143+
d: Dict[str, Any]
137144
baz: Optional[List[List[str]]]
138145
""")
139146
}

test/test_generator/test_detect_type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
pytest.param("1", IntString, id="int_str"),
1818
pytest.param("1.0", FloatString, id="float_str"),
1919
pytest.param("true", BooleanString, id="bool_str"),
20-
pytest.param({"test_dict_field_a": 1, "test_dict_field_b": "a"}, DDict(DUnion(int, str)), id="dict")
20+
pytest.param({"test_dict_field_a": 1, "test_dict_field_b": "a"}, DDict(DUnion(int, str)), id="dict"),
21+
pytest.param({}, DDict(Unknown))
2122
]
2223

2324
test_dict = {param.id: param.values[0] for param in test_data}

0 commit comments

Comments
 (0)