Skip to content

Commit 59c8e39

Browse files
committed
Merge remote-tracking branch 'origin/master' into eval_generated_code
2 parents 7664f2e + bb6da51 commit 59c8e39

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

json_to_models/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def main(version_string=None):
300300
cli.parse_args()
301301
if not version_string:
302302
version_string = (
303-
'"""\n'
303+
'r"""\n'
304304
f'generated by json2python-models v{json_to_models.__version__} at {datetime.now().ctime()}\n'
305305
f'command: {" ".join(sys.argv)}\n'
306306
'"""\n'

json_to_models/dynamic_typing/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .base import (
2-
BaseType, ImportPathList, MetaData, NoneType, Unknown, UnknownType, get_hash_string
2+
BaseType, ImportPathList, MetaData, Null, Unknown, get_hash_string
33
)
44
from .complex import ComplexType, DDict, DList, DOptional, DTuple, DUnion, SingleType
55
from .models_meta import AbsoluteModelRef, ModelMeta, ModelPtr

json_to_models/dynamic_typing/base.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,27 @@ def to_hash_string(self) -> str:
7070
return "Unknown"
7171

7272

73+
class NoneType(BaseType):
74+
__slots__ = []
75+
76+
def __str__(self):
77+
return "NoneType"
78+
79+
def __iter__(self) -> Iterable['MetaData']:
80+
return iter(tuple())
81+
82+
def replace(self, t: 'MetaData', **kwargs) -> 'NoneType':
83+
return self
84+
85+
def to_typing_code(self) -> Tuple[ImportPathList, str]:
86+
return ([], 'None')
87+
88+
def to_hash_string(self) -> str:
89+
return "NoneType"
90+
91+
7392
Unknown = UnknownType()
74-
NoneType = type(None)
93+
Null = NoneType()
7594
MetaData = Union[type, dict, BaseType]
7695

7796

json_to_models/generator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from unidecode import unidecode
77

8-
from .dynamic_typing import (ComplexType, DDict, DList, DOptional, DUnion, MetaData, ModelPtr, NoneType, SingleType,
8+
from .dynamic_typing import (ComplexType, DDict, DList, DOptional, DUnion, MetaData, ModelPtr, Null, SingleType,
99
StringSerializable, StringSerializableRegistry, Unknown, registry)
1010

1111
keywords_set = set(keyword.kwlist)
@@ -105,7 +105,7 @@ def _detect_type(self, value, convert_dict=True) -> MetaData:
105105

106106
# null interpreted as is and will be processed later on Union merge stage
107107
elif value is None:
108-
return NoneType
108+
return Null
109109

110110
# string types trying to convert to other string-serializable types
111111
else:
@@ -211,7 +211,7 @@ def _optimize_union(self, t: DUnion):
211211
for item in t.types:
212212
if isinstance(item, DOptional):
213213
item = item.type
214-
other_types.append(NoneType)
214+
other_types.append(Null)
215215
if isinstance(item, dict):
216216
types_to_merge.append(item)
217217
elif item in self.str_types_registry or item is str:
@@ -248,10 +248,10 @@ def _optimize_union(self, t: DUnion):
248248
types.remove(Unknown)
249249

250250
optional = False
251-
if NoneType in types:
251+
if Null in types:
252252
optional = True
253-
while NoneType in types:
254-
types.remove(NoneType)
253+
while Null in types:
254+
types.remove(Null)
255255

256256
if len(types) > 1:
257257
meta_type = DUnion(*types)

test/test_code_generation/test_typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TestModel:
6363
# MetaData | Tuple[import_stmnt, type]
6464
test_data = [
6565
pytest.param(
66-
UnknownType(),
66+
Unknown,
6767
('from typing import Any', Any),
6868
id="UnknownType"
6969
),
@@ -78,7 +78,7 @@ class TestModel:
7878
id="DOptional"
7979
),
8080
pytest.param(
81-
DOptional(UnknownType()),
81+
DOptional(Unknown),
8282
('from typing import Any, Optional', Optional[Any]),
8383
id="DOptional_UnknownType"
8484
),

test/test_generator/test_detect_type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from json_to_models.dynamic_typing import BooleanString, DDict, DList, DUnion, FloatString, IntString, NoneType, Unknown
3+
from json_to_models.dynamic_typing import BooleanString, DDict, DList, DUnion, FloatString, IntString, Null, Unknown
44
from json_to_models.generator import MetadataGenerator
55

66
# JSON data | MetaData
@@ -9,7 +9,7 @@
99
pytest.param(1, int, id="int"),
1010
pytest.param(True, bool, id="bool"),
1111
pytest.param("abc", str, id="str"),
12-
pytest.param(None, NoneType, id="null"),
12+
pytest.param(None, Null, id="null"),
1313
pytest.param([], DList(Unknown), id="list_empty"),
1414
pytest.param([1], DList(int), id="list_single"),
1515
pytest.param([*range(100)], DList(int), id="list_single_type"),

test/test_generator/test_optimize_type.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from json_to_models.dynamic_typing import (BooleanString, DDict, DList, DOptional, DTuple, DUnion, FloatString,
4-
IntString, NoneType, Unknown)
4+
IntString, Null, Unknown)
55
from json_to_models.generator import MetadataGenerator
66

77
# MetaData | Optimized MetaData
@@ -16,7 +16,7 @@
1616
{'1': DUnion(
1717
{'a': DUnion({'b': int}, {'b': float})},
1818
{'a': DUnion({'b': float}, {'b': int})},
19-
{'a': NoneType},
19+
{'a': Null},
2020
)},
2121
{'1': {'a': DOptional({'b': float})}},
2222
id="merge_nested_dicts"
@@ -72,32 +72,32 @@
7272
id="list_unknown_vs_multi"
7373
),
7474
pytest.param(
75-
DUnion(NoneType, str),
75+
DUnion(Null, str),
7676
DOptional(str),
7777
id="optional_str"
7878
),
7979
pytest.param(
80-
DUnion(NoneType, DList(str)),
80+
DUnion(Null, DList(str)),
8181
DOptional(DList(str)),
8282
id="optional_list"
8383
),
8484
pytest.param(
85-
DList(DUnion(NoneType, str)),
85+
DList(DUnion(Null, str)),
8686
DList(DOptional(str)),
8787
id="list_of_optional_strings"
8888
),
8989
pytest.param(
90-
DUnion(NoneType, DList(str), int),
90+
DUnion(Null, DList(str), int),
9191
DOptional(DUnion(DList(str), int)),
9292
id="optional_list_or_int"
9393
),
9494
pytest.param(
95-
DUnion(NoneType, DList(DUnion(str, int))),
95+
DUnion(Null, DList(DUnion(str, int))),
9696
DOptional(DList(DUnion(str, int))),
9797
id="optional_list_of_str_or_int"
9898
),
9999
pytest.param(
100-
DList(DUnion(NoneType, str, int)),
100+
DList(DUnion(Null, str, int)),
101101
DList(DOptional(DUnion(str, int))),
102102
id="list_of_optional_strings_ot_int"
103103
),
@@ -112,7 +112,7 @@
112112
id="optional_union_nested"
113113
),
114114
pytest.param(
115-
DUnion(NoneType, str, NoneType),
115+
DUnion(Null, str, Null),
116116
DOptional(str),
117117
id="optional_str"
118118
),

0 commit comments

Comments
 (0)