Skip to content

Commit efc3c88

Browse files
final round of review improvements
1 parent bb23d01 commit efc3c88

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

utils/generator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ def add_attribute(self, k, arg, for_types_py=False):
289289
# insert in the right place so that all required arguments
290290
# appear at the top of the argument list
291291
i = 0
292-
for i, arg in enumerate(k["args"]):
292+
for i in range(len(k["args"]) + 1):
293+
if i == len(k["args"]):
293294
break
294295
if k["args"][i].get("positional"):
295296
continue
@@ -475,11 +476,12 @@ def interface_to_python_class(self, interface, interfaces):
475476
raise RuntimeError(f"Type {interface} is not an interface")
476477
k = {"name": interface, "args": []}
477478
while True:
478-
if "inherits" not in type_ or "type" not in type_["inherits"]:
479-
break
480-
481479
for arg in type_["properties"]:
482480
schema.add_attribute(k, arg, for_types_py=True)
481+
482+
if "inherits" not in type_ or "type" not in type_["inherits"]:
483+
break
484+
483485
if "parent" not in k:
484486
k["parent"] = type_["inherits"]["type"]["name"]
485487
if type_["inherits"]["type"]["name"] not in interfaces:

utils/templates/query.py.tpl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,47 +153,51 @@ class {{ k.name }}({{ parent }}):
153153
{{ line }}
154154
{% endfor %}
155155
{% if k.args %}
156-
{% if k.docstring %}
156+
{% if k.docstring %}
157157

158-
{% endif %}
159-
{% for kwarg in k.args %}
160-
{% for line in kwarg.doc %}
158+
{% endif %}
159+
{% for kwarg in k.args %}
160+
{% for line in kwarg.doc %}
161161
{{ line }}
162-
{% endfor %}
163-
{% endfor %}
162+
{% endfor %}
163+
{% endfor %}
164164
{% endif %}
165165
"""
166166
name = "{{ k.property_name }}"
167167
{% if k.params %}
168168
_param_defs = {
169-
{% for param in k.params %}
169+
{% for param in k.params %}
170170
"{{ param.name }}": {{ param.param }},
171-
{% endfor %}
172-
{% if k.name == "FunctionScore" %}
171+
{% endfor %}
172+
{% if k.name == "FunctionScore" %}
173+
{# The FunctionScore class implements a custom solution for the `functions`
174+
shortcut property. Until the code generator can support shortcut
175+
properties directly that solution is added here #}
173176
"filter": {"type": "query"},
174177
"functions": {"type": "score_function", "multi": True},
175-
{% endif %}
178+
{% endif %}
176179
}
177180
{% endif %}
178181

179182
def __init__(
180183
self,
181184
{% for arg in k.args %}
182-
{% if arg.positional %}
185+
{% if arg.positional %}
183186
{{ arg.name }}: {{ arg.type }} = DEFAULT,
184-
{% endif %}
187+
{% endif %}
185188
{% endfor %}
186189
{% if k.args and not k.args[-1].positional %}
187190
*,
188191
{% endif %}
189192
{% for arg in k.args %}
190-
{% if not arg.positional %}
193+
{% if not arg.positional %}
191194
{{ arg.name }}: {{ arg.type }} = DEFAULT,
192-
{% endif %}
195+
{% endif %}
193196
{% endfor %}
194197
**kwargs: Any
195198
):
196199
{% if k.name == "FunctionScore" %}
200+
{# continuation of the FunctionScore shortcut property support from above #}
197201
if functions is DEFAULT:
198202
functions = []
199203
for name in ScoreFunction._classes:
@@ -209,13 +213,15 @@ class {{ k.name }}({{ parent }}):
209213
{% endif %}
210214
super().__init__(
211215
{% for arg in k.args %}
212-
{% if not arg.positional %}
216+
{% if not arg.positional %}
213217
{{ arg.name }}={{ arg.name }},
214-
{% endif %}
218+
{% endif %}
215219
{% endfor %}
216220
**kwargs
217221
)
218222

223+
{# what follows is a set of Pythonic enhancements to some of the query classes
224+
which are outside the scope of the code generator #}
219225
{% if k.name == "MatchAll" %}
220226
def __add__(self, other: "Query") -> "Query":
221227
return other._clone()

utils/templates/types.py.tpl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ PipeSeparatedFlags = str
2727

2828

2929
{% for k in classes %}
30-
class {{ k.name }}({ k.parent if k.parent else "AttrDict[Any]" }}):
30+
class {{ k.name }}({{ k.parent if k.parent else "AttrDict[Any]" }}):
3131
{% if k.args %}
3232
"""
33-
{% for arg in k.args %}
34-
{% for line in arg.doc %}
33+
{% for arg in k.args %}
34+
{% for line in arg.doc %}
3535
{{ line }}
36-
{% endfor %}
37-
{% endfor %}
36+
{% endfor %}
37+
{% endfor %}
3838
"""
39-
{% for arg in k.args %}
39+
{% for arg in k.args %}
4040
{{ arg.name }}: {{ arg.type }}
41-
{% endfor %}
41+
{% endfor %}
4242

4343
def __init__(
4444
self,
@@ -62,14 +62,14 @@ class {{ k.name }}({ k.parent if k.parent else "AttrDict[Any]" }}):
6262
kwargs[str(field)] = value
6363
{% endif %}
6464
{% for arg in k.args %}
65-
{% if not arg.positional %}
65+
{% if not arg.positional %}
6666
if {{ arg.name }} is not DEFAULT:
67-
{% if "InstrumentedField" in arg.type %}
67+
{% if "InstrumentedField" in arg.type %}
6868
kwargs["{{ arg.name }}"] = str({{ arg.name }})
69-
{% else %}
69+
{% else %}
7070
kwargs["{{ arg.name }}"] = {{ arg.name }}
71+
{% endif %}
7172
{% endif %}
72-
{% endif %}
7373
{% endfor %}
7474
{% if k.parent %}
7575
super().__init__(**kwargs)

0 commit comments

Comments
 (0)