Skip to content

Commit

Permalink
final round of review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Sep 11, 2024
1 parent bb23d01 commit efc3c88
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
10 changes: 6 additions & 4 deletions utils/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ def add_attribute(self, k, arg, for_types_py=False):
# insert in the right place so that all required arguments
# appear at the top of the argument list
i = 0
for i, arg in enumerate(k["args"]):
for i in range(len(k["args"]) + 1):
if i == len(k["args"]):
break
if k["args"][i].get("positional"):
continue
Expand Down Expand Up @@ -475,11 +476,12 @@ def interface_to_python_class(self, interface, interfaces):
raise RuntimeError(f"Type {interface} is not an interface")
k = {"name": interface, "args": []}
while True:
if "inherits" not in type_ or "type" not in type_["inherits"]:
break

for arg in type_["properties"]:
schema.add_attribute(k, arg, for_types_py=True)

if "inherits" not in type_ or "type" not in type_["inherits"]:
break

if "parent" not in k:
k["parent"] = type_["inherits"]["type"]["name"]
if type_["inherits"]["type"]["name"] not in interfaces:
Expand Down
38 changes: 22 additions & 16 deletions utils/templates/query.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -153,47 +153,51 @@ class {{ k.name }}({{ parent }}):
{{ line }}
{% endfor %}
{% if k.args %}
{% if k.docstring %}
{% if k.docstring %}

{% endif %}
{% for kwarg in k.args %}
{% for line in kwarg.doc %}
{% endif %}
{% for kwarg in k.args %}
{% for line in kwarg.doc %}
{{ line }}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endif %}
"""
name = "{{ k.property_name }}"
{% if k.params %}
_param_defs = {
{% for param in k.params %}
{% for param in k.params %}
"{{ param.name }}": {{ param.param }},
{% endfor %}
{% if k.name == "FunctionScore" %}
{% endfor %}
{% if k.name == "FunctionScore" %}
{# The FunctionScore class implements a custom solution for the `functions`
shortcut property. Until the code generator can support shortcut
properties directly that solution is added here #}
"filter": {"type": "query"},
"functions": {"type": "score_function", "multi": True},
{% endif %}
{% endif %}
}
{% endif %}

def __init__(
self,
{% for arg in k.args %}
{% if arg.positional %}
{% if arg.positional %}
{{ arg.name }}: {{ arg.type }} = DEFAULT,
{% endif %}
{% endif %}
{% endfor %}
{% if k.args and not k.args[-1].positional %}
*,
{% endif %}
{% for arg in k.args %}
{% if not arg.positional %}
{% if not arg.positional %}
{{ arg.name }}: {{ arg.type }} = DEFAULT,
{% endif %}
{% endif %}
{% endfor %}
**kwargs: Any
):
{% if k.name == "FunctionScore" %}
{# continuation of the FunctionScore shortcut property support from above #}
if functions is DEFAULT:
functions = []
for name in ScoreFunction._classes:
Expand All @@ -209,13 +213,15 @@ class {{ k.name }}({{ parent }}):
{% endif %}
super().__init__(
{% for arg in k.args %}
{% if not arg.positional %}
{% if not arg.positional %}
{{ arg.name }}={{ arg.name }},
{% endif %}
{% endif %}
{% endfor %}
**kwargs
)

{# what follows is a set of Pythonic enhancements to some of the query classes
which are outside the scope of the code generator #}
{% if k.name == "MatchAll" %}
def __add__(self, other: "Query") -> "Query":
return other._clone()
Expand Down
22 changes: 11 additions & 11 deletions utils/templates/types.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ PipeSeparatedFlags = str


{% for k in classes %}
class {{ k.name }}({ k.parent if k.parent else "AttrDict[Any]" }}):
class {{ k.name }}({{ k.parent if k.parent else "AttrDict[Any]" }}):
{% if k.args %}
"""
{% for arg in k.args %}
{% for line in arg.doc %}
{% for arg in k.args %}
{% for line in arg.doc %}
{{ line }}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
"""
{% for arg in k.args %}
{% for arg in k.args %}
{{ arg.name }}: {{ arg.type }}
{% endfor %}
{% endfor %}

def __init__(
self,
Expand All @@ -62,14 +62,14 @@ class {{ k.name }}({ k.parent if k.parent else "AttrDict[Any]" }}):
kwargs[str(field)] = value
{% endif %}
{% for arg in k.args %}
{% if not arg.positional %}
{% if not arg.positional %}
if {{ arg.name }} is not DEFAULT:
{% if "InstrumentedField" in arg.type %}
{% if "InstrumentedField" in arg.type %}
kwargs["{{ arg.name }}"] = str({{ arg.name }})
{% else %}
{% else %}
kwargs["{{ arg.name }}"] = {{ arg.name }}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% if k.parent %}
super().__init__(**kwargs)
Expand Down

0 comments on commit efc3c88

Please sign in to comment.