Skip to content

Commit 3006eed

Browse files
authored
Merge pull request #28 from bogdandm/more-reserved-keywords
Add more reserved words
2 parents 330c8f3 + d125bd3 commit 3006eed

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

json_to_models/models/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"{% endfor %}"
2222

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

2629

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

244247

245248
def prepare_label(s: str, convert_unicode: bool) -> str:
246-
if s in keywords_set:
249+
if s in blacklist_words:
247250
s += "_"
248251
if convert_unicode:
249252
s = unidecode(s)

test/test_code_generation/test_attrs_generation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ class Test:
121121
"body": "attr.ib(default=None)"
122122
},
123123
"dict": {
124-
"name": "dict",
124+
"name": "dict_",
125125
"type": "Dict[str, int]",
126-
"body": "attr.ib()"
126+
"body": f"attr.ib({field_meta('dict')})"
127127
},
128128
"not": {
129129
"name": "not_",
@@ -154,7 +154,7 @@ class Test:
154154
class Test:
155155
foo: int = attr.ib()
156156
qwerty: FloatString = attr.ib()
157-
dict: Dict[str, int] = attr.ib()
157+
dict_: Dict[str, int] = attr.ib({field_meta('dict')})
158158
not_: bool = attr.ib({field_meta('not')})
159159
one_day: int = attr.ib({field_meta('1day')})
160160
den_nedeli: str = attr.ib({field_meta('день_недели')})

test/test_code_generation/test_dataclasses_generation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ class Test:
9494
"body": "None"
9595
},
9696
"dict": {
97-
"name": "dict",
98-
"type": "Dict[str, int]"
97+
"name": "dict_",
98+
"type": "Dict[str, int]",
99+
"body": f"field({field_meta('dict')})"
99100
}
100101
},
101-
"generated": trim("""
102+
"generated": trim(f"""
102103
from dataclasses import dataclass, field
103104
from json_to_models.dynamic_typing import FloatString, IntString
104105
from json_to_models.models import ClassType
@@ -111,7 +112,7 @@ class Test:
111112
class Test:
112113
foo: int
113114
qwerty: FloatString
114-
dict: Dict[str, int]
115+
dict_: Dict[str, int] = field({field_meta('dict')})
115116
baz: Optional[List[List[str]]] = field(default_factory=list)
116117
bar: Optional[IntString] = None
117118
asdfg: Optional[int] = None

0 commit comments

Comments
 (0)