Type hints in shared.views.autocomplete

This commit is contained in:
Martin Pépin 2020-01-02 17:07:50 +01:00
parent 914888d18a
commit d2c6c9da7a
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -1,18 +1,22 @@
from dal import autocomplete
from django.db.models import Q
from typing import Generic, Iterable, Type, TypeVar
from dal import autocomplete # type: ignore
from django.db.models import Model, Q
M = TypeVar("M", bound=Model)
class ModelSearch:
class ModelSearch(Generic[M]):
"""Basic search engine for models based on filtering.
Subclasses should override the ``model`` class attribute and specify the list of
search fields to be searched in.
"""
model = None
search_fields = []
model: Type[M]
search_fields: Iterable[str]
def get_queryset_filter(self, keywords):
def get_queryset_filter(self, keywords: Iterable[str]) -> Q:
filter_q = Q()
if not keywords:
@ -26,7 +30,7 @@ class ModelSearch:
return filter_q
def search(self, keywords):
def search(self, keywords: Iterable[str]) -> Iterable[M]:
"""Returns the queryset of model instances matching all the keywords.
The semantic of the search is the following: a model instance appears in the