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

DslBase.__init__() typing won't accept dict unpacking #1896

Open
realsuayip opened this issue Sep 3, 2024 · 1 comment
Open

DslBase.__init__() typing won't accept dict unpacking #1896

realsuayip opened this issue Sep 3, 2024 · 1 comment

Comments

@realsuayip
Copy link

DslBase.__init__() typing does not accept dict unpacking, hence all Query subclasses.

def __init__(self, _expand__to_dot: Optional[bool] = None, **params: Any) -> None:

For example, Term(**my_dict) will result in following mypy error:

error: Argument 1 to "Term" has incompatible type "**Dict[str, str]"; expected "Optional[bool]

This is due to _expand__to_dot, mypy assumes all params have unified type. Maybe just use **params: Any and pop _expand__to_dot from there?

@miguelgrinberg
Copy link
Collaborator

miguelgrinberg commented Sep 3, 2024

This will be addressed after #1890 is merged. All query classes are going to have explicit arguments. Here is how Term is going to be defined:

class Term(Query):
    """
    Returns documents that contain an exact term in a provided field. To
    return a document, the query term must exactly match the queried
    field's value, including whitespace and capitalization.

    :arg _field: The field to use in this query.
    :arg _value: The query value for the field.
    """

    name = "term"

    def __init__(
        self,
        _field: Union[str, "InstrumentedField", "NotSet"] = NOT_SET,
        _value: Union["i.TermQuery", Dict[str, Any], "NotSet"] = NOT_SET,
        **kwargs: Any,
    ):
        if not isinstance(_field, NotSet):
            kwargs[str(_field)] = _value
        super().__init__(**kwargs)

If you want to take advantage of typing, then you would call Term(field, value), but you can also use the current form and do Term(**kwargs). The _expand__to_dot will still be supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants