Skip to content
Merged
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
5 changes: 4 additions & 1 deletion json_to_models/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"{% endfor %}"

keywords_set = set(keyword.kwlist)
builtins_set = set(__builtins__.keys())
other_common_names_set = {'datetime', 'time', 'date', 'defaultdict'}
blacklist_words = frozenset(keywords_set | builtins_set | other_common_names_set)
ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']


Expand Down Expand Up @@ -243,7 +246,7 @@ def sort_kwargs(kwargs: dict, ordering: Iterable[Iterable[str]]) -> dict:


def prepare_label(s: str, convert_unicode: bool) -> str:
if s in keywords_set:
if s in blacklist_words:
s += "_"
if convert_unicode:
s = unidecode(s)
Expand Down
6 changes: 3 additions & 3 deletions test/test_code_generation/test_attrs_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class Test:
"body": "attr.ib(default=None)"
},
"dict": {
"name": "dict",
"name": "dict_",
"type": "Dict[str, int]",
"body": "attr.ib()"
"body": f"attr.ib({field_meta('dict')})"
},
"not": {
"name": "not_",
Expand Down Expand Up @@ -154,7 +154,7 @@ class Test:
class Test:
foo: int = attr.ib()
qwerty: FloatString = attr.ib()
dict: Dict[str, int] = attr.ib()
dict_: Dict[str, int] = attr.ib({field_meta('dict')})
not_: bool = attr.ib({field_meta('not')})
one_day: int = attr.ib({field_meta('1day')})
den_nedeli: str = attr.ib({field_meta('день_недели')})
Expand Down
9 changes: 5 additions & 4 deletions test/test_code_generation/test_dataclasses_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ class Test:
"body": "None"
},
"dict": {
"name": "dict",
"type": "Dict[str, int]"
"name": "dict_",
"type": "Dict[str, int]",
"body": f"field({field_meta('dict')})"
}
},
"generated": trim("""
"generated": trim(f"""
from dataclasses import dataclass, field
from json_to_models.dynamic_typing import FloatString, IntString
from json_to_models.models import ClassType
Expand All @@ -111,7 +112,7 @@ class Test:
class Test:
foo: int
qwerty: FloatString
dict: Dict[str, int]
dict_: Dict[str, int] = field({field_meta('dict')})
baz: Optional[List[List[str]]] = field(default_factory=list)
bar: Optional[IntString] = None
asdfg: Optional[int] = None
Expand Down