Skip to content
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

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Jan 7, 2025

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's ScoreFunction class which implements this functionality idiomatically.

Copy link
Member

@pquentin pquentin left a 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},
Copy link
Member

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?

Copy link
Collaborator Author

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.

@miguelgrinberg miguelgrinberg added the backport 8.x Backport to 8.x label Jan 7, 2025
@miguelgrinberg miguelgrinberg merged commit 5d2bccd into elastic:main Jan 7, 2025
19 checks passed
@miguelgrinberg miguelgrinberg deleted the function-score-container-generator branch January 7, 2025 12:42
github-actions bot pushed a commit that referenced this pull request Jan 7, 2025
miguelgrinberg added a commit that referenced this pull request Jan 7, 2025
Fixes #1957

(cherry picked from commit 5d2bccd)

Co-authored-by: Miguel Grinberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot use FunctionScoreContainer?
2 participants