-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct typing hints for the FunctionScore query #1960
Correct typing hints for the FunctionScore query #1960
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM.
@@ -612,9 +612,9 @@ class FunctionScore(Query): | |||
|
|||
name = "function_score" | |||
_param_defs = { | |||
"functions": {"type": "score_function", "multi": True}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own understanding, how does this line get added, since we removed it in query.py.tpl?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed a bit "magical" to me as well when I noticed it and made me smile. It is all done by logic that is already present in the generator.
In the original version before this fix I had to put this definition in the template, because the code generator did not attempt to do anything with function scores.
But with this fix the generator recognizes FunctionScoreContainer
and returns a DSL type for it. The return value has two components, the typing hint and the DSL type:
return "ScoreFunction", {"type": "score_function"}
The first returned value is used in type hints. The second one (when given, since it is optional) is added to the _param_defs
dictionary of the class by the generator itself. After this type is seen the generator will likely see an array_of
that wraps this type, and that adds Sequence[...]
to the type hint, and "multi": True
to the DSL type.
The intention is that at some point all these will be generated, but there are still some that are manually injected through the templates.
Fixes #1957 (cherry picked from commit 5d2bccd) Co-authored-by: Miguel Grinberg <[email protected]>
Fixes #1957
The fix is simple, the code generator needed to intercept references to
FunctionScoreContainer
, halt code generation at that point and direct those references to the DSL'sScoreFunction
class which implements this functionality idiomatically.