Drop type hints in shared.views.autocomplete

This commit is contained in:
Martin Pépin 2020-02-12 19:01:08 +01:00
parent a259b04d9c
commit b8cd5f1da5
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -1,20 +1,13 @@
from typing import Generic, Iterable, Type, TypeVar from dal import autocomplete
from django.db.models import Q
from dal import autocomplete # type: ignore
from django.db.models import Model, Q
M = TypeVar("M", bound=Model)
class ModelSearch(Generic[M]): class ModelSearch:
"""Basic search engine for models based on filtering. """Basic search engine for models based on filtering.
As the type hints indicate, the class is generic with respect to the model. This The class should be configured through its ``model`` class attribute: the ``search``
means that the ``search`` method only returns instances of the model specified as method will return a queryset of instances of this model. The ``search_fields``
the ``model`` class attribute in subclasses. attributes indicates which fields to search in.
The ``search_fields`` attributes indicates which fields to search in during the
search.
Example: Example:
@ -28,12 +21,10 @@ class ModelSearch(Generic[M]):
>>> user_search.search(["toto", "foo"]) # returns a queryset of Users >>> user_search.search(["toto", "foo"]) # returns a queryset of Users
""" """
# This says that `model` is the class corresponding to the type variable M (or a model = None
# subclass). search_fields = []
model: Type[M]
search_fields: Iterable[str]
def get_queryset_filter(self, keywords: Iterable[str]) -> Q: def get_queryset_filter(self, keywords):
filter_q = Q() filter_q = Q()
if not keywords: if not keywords:
@ -47,7 +38,7 @@ class ModelSearch(Generic[M]):
return filter_q return filter_q
def search(self, keywords: Iterable[str]) -> Iterable[M]: def search(self, keywords):
"""Returns the queryset of model instances matching all the keywords. """Returns the queryset of model instances matching all the keywords.
The semantic of the search is the following: a model instance appears in the The semantic of the search is the following: a model instance appears in the