Skip to content

Cannot use FunctionScoreContainer? #1957

Closed
@philbudne

Description

@philbudne

While trying to upgrade a project to version 8.17, existing code:

from elasticsearch_dsl import Search
from elasticsearch_dsl.function import RandomScore
from elasticsearch_dsl.query import FunctionScore

s = Search().query(
    FunctionScore(
        functions=[
            RandomScore()
        ]
    )
)

print(s.to_dict())

outputs:

{'query': {'function_score': {'functions': [{'random_score': {}}]}}}

But mypy complains:

test.py:9: error: Argument "functions" to "FunctionScore" has incompatible type "list[RandomScore]"; expected "Sequence[FunctionScoreContainer] | Sequence[dict[str, Any]] | DefaultType"  [arg-type]

mypy is happy with:

from elasticsearch_dsl import Search
from elasticsearch_dsl.function import RandomScore
from elasticsearch_dsl.query import FunctionScore
from elasticsearch_dsl.types import FunctionScoreContainer

s2 = Search().query(
    FunctionScore(
        functions=[
            FunctionScoreContainer(
                random_score=RandomScore()
            )
        ]
    )
)
print(s2.to_dict())

which fails at runtime with:

TypeError: unhashable type: 'FunctionScoreContainer'

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions